php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49521 PDO fetchObject sets values before calling constructor
Submitted: 2009-09-10 11:45 UTC Modified: 2010-03-18 23:08 UTC
Votes:5
Avg. Score:4.6 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: waps at pisem dot net Assigned: pierrick (profile)
Status: Closed Package: PDO related
PHP Version: 5.2.10 OS: Ubuntu 8.10 x64
Private report: No CVE-ID: None
 [2009-09-10 11:45 UTC] waps at pisem dot net
Description:
------------
Incorrect creating user object: set data before call constructor method.

Reproduce code:
---------------
---
From manual page: pdostatement.fetchobject
---

class Product {
    public function __construct() {
        echo 'create object, ';
    }
    public function __set($offset, $value) {
        echo 'set value, ';
    }
}

// fetch object
$stmt->fetchObject('Product', array());

Expected result:
----------------
Expected result: create object, set value, 

Actual result:
--------------
Actual result: set value, create object,

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-21 18:45 UTC] sjoerd@php.net
Confirmed. If the constructor sets default values for some fields, as is typical, the constructor will overwrite the values just retrieved from the database.

<?php
class Book {
	function __construct() {
		$this->title = "Default";
	}
}

$pdo = new PDO('mysql:dbname=books', 'root');
$statement = $pdo->prepare('SELECT * FROM book WHERE title=\'Peopleware\'');
$statement->execute();
$obj = $statement->fetchObject('Book');
echo $obj->title; // Expected: Peopleware. Actual: Default
?> 
 [2009-09-22 07:06 UTC] waps at pisem dot net
sjoerd, yes!

Im my php5 framework phpDays (http://phpdays.sf.net) I faced with this problem. Pleae, fix it in php 5.2.x and 5.3.x and 6.x.x. Thnanks!

P.S. Maybe this problem exists in other php components. Need to code review to find this problem in all components.

P.P.S. Please, tell me about fix this problem in future. Thanks!
 [2009-11-06 17:07 UTC] caferrari at gmail dot com
Thats evil!...

My code with an Ugly solution!

<?php

class TestObject {
	public function __construct($id=0, $name='', $mail=''){
		// if (isset($this->id)) return; // Ugly solution necessary atm to bypass the problem!.
		$this->id 	= $id;
		$this->name 	= $nome;
		$this->mail 	= $mail;
	}
}

$dbh = new PDO('sqlite:'.dirname(__FILE__).'/foo.db');
$qr = $dbh->query("SELECT 1 as id, 'test' as name, 'abc@def.com' as mail");
$x = $qr->fetchObject('TestObject');

print_r($x);

Expected:
TestObject Object ( [id] => 1 [name] => test [mail] => abc@def.com ) 

Actual Result:
TestObject Object ( [id] => 0 [name] => [mail] => )
 [2009-11-15 16:20 UTC] svn@php.net
Automatic comment from SVN on behalf of felipe
Revision: http://svn.php.net/viewvc/?view=revision&revision=290786
Log: - Fixed bug #49521 (PDO fetchObject sets values before calling constructor)
  (patch by Pierrick)
 [2009-11-15 16:23 UTC] felipe@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Thanks for the patch.
 [2010-02-11 22:14 UTC] svn@php.net
Automatic comment from SVN on behalf of johannes
Revision: http://svn.php.net/viewvc/?view=revision&revision=294903
Log: Revert 290786: Fixed bug #49521 (PDO fetchObject sets values before calling
constructor)
 [2010-02-12 00:19 UTC] svn@php.net
Automatic comment from SVN on behalf of johannes
Revision: http://svn.php.net/viewvc/?view=revision&revision=294942
Log: merge 290786: Revert 290786: Fixed bug #49521 (PDO fetchObject sets values
before calling constructor)
 [2010-03-18 21:44 UTC] davey@php.net
-Status: Closed +Status: Re-Opened
 [2010-03-18 21:44 UTC] davey@php.net
According to the ML [1], Johannes, Felipe and Derick all agreed that this fix 
should be reverted, and instead the current 
behavior (values then constructor) should be properly documented. The desired 
behavior can be accomplished using the 
PDO::FETCH_PROPS_LATE option since 5.2.0.

This means commit #290786 must be reverted in 5_2. It was already reverted in 
5_3. (Commit #294903 [2])

[1] http://marc.info/?l=php-internals&m=126451457205904&w=2
[2] http://svn.php.net/viewvc/php/php-src/branches/PHP_5_3/ext/pdo/pdo_stmt.c?
r1=293036&r2=294903
 [2010-03-18 23:07 UTC] felipe@php.net
Automatic comment from SVN on behalf of felipe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=296361
Log: Revert 290786: Fixed bug #49521 (PDO fetchObject sets values before calling constructor)
 [2010-03-18 23:08 UTC] felipe@php.net
-Status: Re-Opened +Status: Closed
 [2010-03-18 23:08 UTC] felipe@php.net
Reverted in 5_2 branch.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 08:01:29 2024 UTC