php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52444 Extending PDOStatement and replacing fetch() apply when using foreach
Submitted: 2010-07-26 12:57 UTC Modified: 2014-01-01 12:48 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: antennen at gmail dot com Assigned:
Status: Open Package: PDO SQLite
PHP Version: Irrelevant OS: All
Private report: No CVE-ID:
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: antennen at gmail dot com
New email:
PHP Version: OS:

 

 [2010-07-26 12:57 UTC] antennen at gmail dot com
Description:
------------
Extending PDOStatement and replacing fetch() doesn't work when iterating using foreach. It calls the built in fetch rather than my overridden one. This can be circumvented using while($r = $s->fetch()) { .. } but I believe this behaviour is unexpected.

Test script:
---------------
class ExtendedStatement extends PDOStatement {
	protected function __construct() {
		$this->setFetchMode(PDO::FETCH_ASSOC);
	}
	public function fetch($fetch_style = PDO::FETCH_ASSOC, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0)
	{
		$r = parent::fetch($fetch_style, $cursor_orientation, $cursor_offset);
		if (is_array($r)) { $r["extradata"] = TRUE; }
		return $r;
	}
}
$db = new PDO("sqlite::memory:");
$db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array("ExtendedStatement", array($db)));
$db->exec("CREATE TABLE example(id INTEGER PRIMARY KEY, name VARCHAR)");
$db->exec("INSERT INTO example(name) VALUES('test')");
$s = $db->prepare("SELECT * FROM example");
$s->execute();
foreach ($s as $r) {
	var_dump($r);
}

Expected result:
----------------
array(3) {
  ["id"]=>
  string(1) "1"
  ["name"]=>
  string(4) "test"
  ["extradata"]=>
  bool(true)
}

Actual result:
--------------
array(2) {
  ["id"]=>
  string(1) "1"
  ["name"]=>
  string(4) "test"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-26 13:31 UTC] johannes@php.net
PDO should rather implement the Iterator interface so one can override "current"
 [2014-01-01 12:48 UTC] felipe@php.net
-Package: PDO related +Package: PDO SQLite
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Fri Mar 24 20:01:40 2017 UTC