ID:               34909
 Comment by:       akorthaus at web dot de
 Reported By:      mcka at gmx dot net
 Status:           Open
 Bug Type:         PDO related
 Operating System: all
 PHP Version:      5.1.0RC3
 New Comment:

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


Previous Comments:
------------------------------------------------------------------------

[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] [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

Reply via email to