Edit report at https://bugs.php.net/bug.php?id=34909&edit=1
ID: 34909 Updated by: ni...@php.net Reported by: mcka at gmx dot net Summary: Defaut FetchMode for PDOStatements in PDO class -Status: Open +Status: Closed Type: Feature/Change Request Package: PDO related Operating System: all PHP Version: 5.1.0RC3 -Assigned To: +Assigned To: nikic Block user comment: N Private report: N New Comment: Closing as this was already implemented in the meantime (see PDO::http://php.net/manual/en/pdo.setattribute.php on http://php.net/manual/en/pdo.setattribute.php). Previous Comments: ------------------------------------------------------------------------ [2005-10-19 14:40:32] akorthaus at web dot de I have posted a simple patch on pecl.php.dev, which implements a PDO::setDefaultFetchMode() methodes, but it's not complete yet, it only supports simple FetchModes, without additional parameters. http://news.php.net/php.pecl.dev/3122 ------------------------------------------------------------------------ [2005-10-18 20:08:53] akorthaus at web dot de That's exactly the same issue I ran into. It would be great to write simple/clean code without setting the fetch mode of every statement/query, when you don't want to use PDO::FETCH_BOTH. This is the only issue which forces me to wrap PDO. And I think it would be a good idea to avoid that by managing a default fetch mode by the PDO API. I have looked at the PDO source and tried to write a patch for that, it does not look very difficult, but it's quite difficult for me because I'm not a C developer ;-) The default fetch mode is hardcoded everywhere. This must be changed to a property of the PDO DBH class/struct. A "PDO::setDefaultFetchMode()" methode could change this value, and it has to be passed to every PDOStatement object created. There must be a property for the default fetch mode in PDOStatement too. So methodes in PDOStatement can check this default fetch mode property, instead of the hardcoded fetch mode. This has also been suggested in the PECL bug-tracker by someone else some time ago: http://pecl.php.net/bugs/bug.php?id=4732 ------------------------------------------------------------------------ [2005-10-18 18:50:27] mcka at gmx dot net Hm, in pdo_dbh.c I can find: /* {{{ proto object PDO::query(string sql [, PDOStatement::setFetchMode() args]) Prepare and execute $sql; returns the statement object for iteration */ static PHP_METHOD(PDO, query) { [...] http://cvs.php.net/co.php/php-src/ext/pdo/pdo_dbh.c?r=1.99#976 But this only saves one simple line in my "PDOwithDefaultFetchMode" class, I still can't completely avoid such a workaround, I still can only use $db->query($sql) when I'm happy with the default FetchMode, and I think that's a problem of the API. The problem is the same why PDOStatement::setFetchMode() exists. It should make working with PDOStatement::fetch()... the same for each FetchMode. A default FetchMode for the whole PDO object is the only way to make PDO::query()... work with the same simple code for other FetchModes beside the default PDO::FETCH_BOTH. Probably it's too late for PHP 5.1, but it would be great to have such a possibility in PDO. In my opinion something like that belongs "behind" the API of a database abstraction, not in userspace code. ------------------------------------------------------------------------ [2005-10-18 18:18:41] w...@php.net I think we have a documentation bug, because I'm pretty sure you can do this: $db->query("select ...", PDO::FETCH_OBJ) ------------------------------------------------------------------------ [2005-10-18 17:34:25] mcka at gmx dot net A small example to illustrate the problem If I want to write some simple code like the code from PDO::query() docs, only with PDO::FETCH_MODE_OBJ (not tested, only to get the idea): <?php $sql = 'SELECT name, colour, calories FROM fruit ORDER BY name'; foreach ($conn->query($sql) as $row) { print $row->name . "\t"; print$row->colour . "\t"; print $row->calories . "\n"; } ?> I have to create at least a PDO wrapper, or extend PDO (with all the disadvantages of doing that): <?php class PDOwithDefaultFetchMode extends PDO { const DEFAULT_FETCH_MODE; public function __construct($dsn, $username, $password , $driver_options) { parent::__construct($dsn, $username, $password , $driver_options); } public function setDefaultFetchMode($fetch_mode) { self::DEFAULT_FETCH_MODE = $fetch_mode; } public function query($sql) { $stmt = parent::query($sql); $stmt->setFetchMode(self::DEFAULT_FETCH_MODE); return $stmt; } } ?> You allways have to do this, if you are not happy with the FetchMode which is selected as "default" by PDO. IMHO code like that should be part of PDO, not userspace. Of course you can add a $stmt->setFetchMode(PDO::FETCH_MODE_OBJ); to every statement in the code, but if you have a lot of statements in the code, and a lot of PHP scripts, I don't think it's a good option. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=34909 -- Edit this bug report at https://bugs.php.net/bug.php?id=34909&edit=1