ID: 34909
User updated by: mcka at gmx dot net
Reported By: mcka at gmx dot net
-Status: Feedback
+Status: Open
Bug Type: PDO related
Operating System: all
PHP Version: 5.1.0RC3
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2005-10-18 18:18:41] [EMAIL PROTECTED]
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.
------------------------------------------------------------------------
[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