php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34909 Defaut FetchMode for PDOStatements in PDO class
Submitted: 2005-10-18 17:07 UTC Modified: 2012-06-19 09:16 UTC
Votes:14
Avg. Score:4.3 ± 0.9
Reproduced:14 of 14 (100.0%)
Same Version:3 (21.4%)
Same OS:8 (57.1%)
From: mcka at gmx dot net Assigned: nikic (profile)
Status: Closed Package: PDO related
PHP Version: 5.1.0RC3 OS: all
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mcka at gmx dot net
New email:
PHP Version: OS:

 

 [2005-10-18 17:07 UTC] 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!


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-18 17:34 UTC] 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 18:18 UTC] wez@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 18:50 UTC] 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 20:08 UTC] 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-19 14:40 UTC] 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
 [2011-02-21 21:36 UTC] jani@php.net
-Package: Feature/Change Request +Package: PDO related
 [2012-06-19 09:16 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2012-06-19 09:16 UTC] nikic@php.net
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).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 15:01:32 2024 UTC