开发者

PDO use in multiple functions - reuse of prepared statement

开发者 https://www.devze.com 2023-03-02 07:32 出处:网络
I have a class \"test\" with 2 functions, one to create the prepared statement and one to execute one or more times. The problem is how do i set the binding in the first one and fill in the vars on th

I have a class "test" with 2 functions, one to create the prepared statement and one to execute one or more times. The problem is how do i set the binding in the first one and fill in the vars on the other function.

class test {

    private static $moduleSql = "SELECT 
                                            id,
                                            module
                                        FROM 
                                            phs_pages
                                        WHERE 
                                            prettyurl = :prettyUrl AND
                                            mainId = :mainId
                                            ";

    private static function pre() {
        $db = Db_Db::getInstance();
        self::$preModuleSql = $db->prepare(self::$moduleSql);
        self::$preModuleSql->bindParam(':prettyUrl', $prettyUrl);
        self::$preModuleSql->bindParam(':mainId', $mainId);
    }

    private static function getClassByDb($mainId = 0, $id = 0) {
        $prettyUrl = self::$parts[$id];
        self::$preModuleSql->execute();
        self::$preModuleSql->debugDumpParams();
        $result = self::$preModuleSql->fetch(PDO::FETCH_ASSOC);

        // get lower level classes

        if (isset(self::开发者_如何学运维$parts[$id + 1])) {
            self::getClassByDb($result['id'], $id + 1);
        } else {
            return $result;
        }
    }

}


You don´t need to bind any parameters in your first function, you need to do that in your second function, right before you execute the prepared statement.

Edit: By the way, you can also call execute with an array that contains your parameters as keys and the values you want to send.


There are so many things wrong with this class definition .. ehh.

It should look like this :

class RepositoryException extends Exception{}

interface iRepository
{
    public function store( $key , PDOStatement  $statement );
    public function fetch( $key );
}

class StatementRepository implements iRepository{

    protected $_pool = array();

    public function store( $key , PDOStatement  $statement )
    {
        $this->_pool[ $key ] = $statement;
    }
    public function fetch( $key )
    {
        if ( !array_key_exists( $key , $this->_pool ) ){
            throw new RepositoryException( 
                "Statement with name '$key' does not exist" );
        }

        return $this->_pool[ $key ];
    }
}

$repo = new StatementRepository;

$foo = new Foo( $repo );
$bar = new Bar( $repo );

$db = new PDO('sqlite::memory:');
$statement = $db->prepare( 'SELECT .... ');

$repo->store( 'bljum' , $statement );

This way all the object which have accepted in the constructor an object which implements iRepository can fetch statements by name.

You class should not have more responsibilities then it needs. If it is for storing prepared statements , then it should have nothing to do with binding parameters to them and executing something.

Here is what Foo class would do :

class Foo
{
    protected $_repo = null;

    public function __construct( iRepository $repo )
    {
        $this->_repo = $repo;
    }

    public function ergo( $x , $y )
    {
        $statement = $this->_repo->fetch('bljum');
        //bind values
        $statement->execute();
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消