Page 1 of 1

solved. Error while trying to Extend PDO

Posted: Fri May 29, 2009 3:23 pm
by spamyboy
There isn't really much to comment. I can't figure out what is proper way to Extend PDO.

Code: Select all

 
/**
 * Extends PDO and logs all queries that are executed and how long
 * they take, including queries issued via prepared statements
 */
class ExtendedPDO extends PDO {
 
    /**
     * constructor
     * @param string $dsn
     * @param string $username
     * @param string $password
     */
 
    public function __construct($dsn, $username, $password) {
        parent::__construct($dsn, $username, $password);
        $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('ExtendedPDOStatement', array()));
    }
    // write your methods here, eg. 
}
 
class ExtendedPDOStatement extends ExtendedPDO {
    // write your methods here
}
 

Code: Select all

<b>Warning</b>: PDO::setAttribute() [<a href='pdo.setattribute'>pdo.setattribute</a>]: SQLSTATE[HY000]: General error: user-supplied statement class must be derived from PDOStatement in <b>C:\Program Files\Zend\Apache2\htdocs\gcms\gcms\applications\backend\framework.php</b> on line <b>214</b><br />

Re: Error while trying to Extend PDO

Posted: Sat May 30, 2009 4:20 am
by spamyboy
I have understood my last mistake, but now I have stucked on something else.

Code: Select all

 
/**
 * Extends PDO and logs all queries that are executed and how long
 * they take, including queries issued via prepared statements
 */
class ExtendedPDO extends PDO
{
    
    # logged queries with their execution time
    public static $log = array();
    
    /**
     * constructor
     *
     * @param string $dsn
     * @param string $username
     * @param string $password
     * @param array $driver_options
     * @return void
     */
    public function __construct($dsn, $username, $password, $driver_options = array())
    {
        parent::__construct($dsn, $username, $password, $driver_options);
        $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('ExtendedPDOStatement', array($this)));
    }
    
    /**
     * Print out log when all references to a PDO object are remowed.
     *
     * @return void
     */
    public function __destruct()
    {
        self::printLog();
    }
    
    /**
     * When query is called, record the time it takes and then log the query.
     *
     * @param string $query
     * @return PDOStatement object
     */
    public function query($query)
    {
        $start  = microtime(true);
        $result = parent::query($query);
        $time   = microtime(true) - $start;
        LoggedPDO::$log[] = array(
                                  'query' => $query,
                                  'time' => round($time * 1000, 3)
                                  );
        die(var_dump($result));
        return $result;
    }
    
    /**
     * Print out gathered data about queries
     *
     * @return void
     */
    private static function printLog()
    {
        print_r(self::$log);
    }
}
 
class ExtendedPDOStatement extends PDOStatement
{
    public $dbh;
    
    // Actualy, shouldn't this be a (PDOStatement $dbh) object?
    protected function __construct(ExtendedPDO $dbh) {
        $this->dbh = $dbh;
    }
    
    /**
     * When execute is called, record the time it takes and then log the query.
     */
    public function execute($parameters = array())
    {
        return $this->dbh->execute($parameters);
    }
}
 

Code: Select all

 
Fatal error: Call to undefined method ExtendedPDO::execute() in C:\Program Files\Zend\Apache2\htdocs\gcms\gcms\applications\backend\framework.php on line 278
Fatal error: Call to undefined method ExtendedPDO::execute() in C:\Program Files\Zend\Apache2\htdocs\gcms\gcms\applications\backend\framework.php on line 278