php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #56473 Allow PDO_ATTR_STATEMENT_CLASS to be set by PDO::setAttribute()
Submitted: 2005-08-03 11:10 UTC Modified: 2005-09-01 09:26 UTC
From: akorthaus at web dot de Assigned:
Status: Wont fix Package: PDO (PECL)
PHP Version: Irrelevant OS: all
Private report: No CVE-ID: None
 [2005-08-03 11:10 UTC] akorthaus at web dot de
Description:
------------
If you want to use an own Statement-Class which should be returned by PDO::prepare(), you have to do it this way:

$pdo->prepare($sql, array(PDO_ATTR_STATEMENT_CLASS=>array('MyStatementClass')));

see: http://cvs.php.net/co.php/php-src/ext/pdo/tests/pdo_023.phpt

You have to write that everywhere in your code and it's not possible to use 'MyStatementClass' with PDO::query().

What about implementing an attribute which could be set by PDO::setAttribute(), which makes it possible to set the Statement Class once at the beginning of the script? As side effect other methodes like PDO::query() could use that attribute and return the same class.

Any chance to see something like that in PHP 5.1?

Btw.: why is the value of PDO_ATTR_STATEMENT_CLASS of type array and not string?

Expected result:
----------------
I think that would be very useful:

class MyStatementClass extends PDOStatement {
  // my adds/changes ...
}

$pdo->setAttribute(
  array(
    PDO_ATTR_STATEMENT_CLASS => 'MyStatementClass'
  )
);


$pdo->prepare($sql); // will return a MyStatementClass object
$pdo->query($sql); // will return a MyStatementClass object too
...

Actual result:
--------------
The way it is today:

class MyStatementClass extends PDOStatement {
  // my adds/changes ...
}

$pdo->prepare($sql, array(
    PDO_ATTR_STATEMENT_CLASS=>array('MyStatementClass'))
); // will return a MyStatementClass object

$pdo->query($sql); // will not return a MyStatementClass object
$pdo->prepare($sql); // will not return a MyStatementClass object too


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-08-30 23:26 UTC] wez@php.net
You should wrap up the PDO class itself to achieve this.
I know the docs are lacking, but you can take a look in the pdo/tests directory to find out more.
 [2005-09-01 09:26 UTC] akorthaus at web dot de
I have looked at the tests and I have looked at the PDO source what and how it is implemented. 

I can't understand why PDO::query() and PDO::prepare() are treated in a different way here. Why can I set an own statement class in PDO::prepare() and no statement class in PDO::query()? Both methodes of the same class return a statement object. So in my opinion it looks sensible to make both methodes consistent. And I think the best way would be to pull that out of these methodes into an own methode/attribute (or as an alternative way to set the satement class).

In my case this issue is not very important today, because I can live with the PDO Statement class (but I can think of many problems where this would be very useful). 

I know that I can wrap/extend the PDO class to change prepare() and query(), but the PDO-API itself could be better here, which is not really complicated to implement I think.

For me this is a "nice to have" feature, much more important would be to implement a default FetchMode (by implementing an additional attribute or methode in PDO class): http://pecl.php.net/bugs/bug.php?id=4732

Because this is the only problem preventing many people (those not using the hardcoded default FetchMode) from using PDO itself in their code.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 23:01:28 2024 UTC