ID: 34909 User updated by: mcka at gmx dot net Reported By: mcka at gmx dot net Status: Open Bug Type: PDO related Operating System: all PHP Version: 5.1.0RC3 New Comment:
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. Previous Comments: ------------------------------------------------------------------------ [2005-10-18 17:07:47] mcka at gmx dot net Description: ------------ If I don't want to use the PDO_FETCH_BOTH FetchMode (which returns an array indexed by both column names and numbers), I have to pass my desired FetchMode to every PDOStatement and/or fetch() call everywhere in the PHP scripts! So why not offer a methode in PDO class which sets the default FetchMode only once in a script like PDO::setDefaultFetchMode(), or at least add an attribute like PDO_DEFAULT_FETCH_MODE which could be set by PDO::setAttribute(PDO_DEFAULT_FETCH_MODE...)? Makes live much easier for people who don't want to use PDO_FETCH_BOTH, but PDO_FETCH_ASSOC, PDO_FETCH_OBJ, PDO_FETCH_NUM... - without forcing them to extend or wrap PDO AND extend or wrap PDOStatement to implement something like that! ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=34909&edit=1