#49521 [Asn->Csd]: PDO fetchObject sets values before calling constructor

From: Date: Sun, 15 Nov 2009 16:23:42 +0000
Subject: #49521 [Asn->Csd]: PDO fetchObject sets values before calling constructor
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-144629@lists.php.net to get a copy of this message
 ID:               49521
 Updated by:       felipe@php.net
 Reported By:      waps at pisem dot net
-Status:           Assigned
+Status:           Closed
 Bug Type:         PDO related
 Operating System: Ubuntu 8.10 x64
 PHP Version:      5.2.10
 Assigned To:      pierrick
 New Comment:

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.


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

[2009-11-15 16:20:37] 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-12 05:00:55] pierrick@php.net

Patch available at:

http://www.adoy.net/php/49521.PHP_5_2.patch
http://www.adoy.net/php/49521.PHP_5_3.patch
http://www.adoy.net/php/49521.PHP_6_0.patch

------------------------------------------------------------------------

[2009-11-06 17:07:46] 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-09-22 07:06:38] 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-09-21 18:45:30] 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
?> 

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/49521

-- 
Edit this bug report at http://bugs.php.net/?id=49521&edit=1



Thread (9 messages)

« previous php.bugs (#144629) next »