Bug #53394 [Asn->Bgs]: ARRAY_AS_PROPS flag is ignored when used with PDOStatement::fetchObject
| From: | felipe@php.net | Date: | Wed, 24 Nov 2010 21:16:37 +0000 |
| Subject: | Bug #53394 [Asn->Bgs]: ARRAY_AS_PROPS flag is ignored when used with PDOStatement::fetchObject | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-156412@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=53394&edit=1
ID: 53394
Updated by: felipe@php.net
Reported by: public at grik dot net
Summary: ARRAY_AS_PROPS flag is ignored when used with
PDOStatement::fetchObject
-Status: Assigned
+Status: Bogus
Type: Bug
Package: SPL related
PHP Version: 5.3.3
Assigned To: felipe
Block user comment: N
Private report: N
New Comment:
Hi, there was a discussion about this, see bug #49521
You could do something like this:
$a = $PDO->query('select id from links');
$a->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'ArraySlice');
$a = $a->fetch();
var_dump($a->id, $a['id']);
Thanks.
Previous Comments:
------------------------------------------------------------------------
[2010-11-24 15:27:19] tony2001@php.net
Felipe, could you check this out plz?
------------------------------------------------------------------------
[2010-11-24 15:25:58] tony2001@php.net
Please try using this snapshot:
http://snaps.php.net/php5.3-latest.tar.gz
For Windows:
http://windows.php.net/snapshots/
------------------------------------------------------------------------
[2010-11-24 13:54:20] crocodile2u at gmail dot com
Or possibly it would be better to change the PDO's behaviour? It seems
very
strange and unnatural that constructor is called after the properties
are set. It
only makes developers write unobvious code for the only purpose of
workaround for
this PDO's feature (personally, I would consider it a bug).
------------------------------------------------------------------------
[2010-11-24 13:46:58] public at grik dot net
I was hinted that PDO sets the fields before calling a constructor.
It makes everything clear.
parent::__construct($array,ArrayObject::ARRAY_AS_PROPS);
is called after setting object fields, and they don't get processed by
ArrayObject::offsetSet.
ArrayObject::оffsetGet after changing mode to ARRAY_AS_PROPS doesn't
see the fields set by the PDO hack.
оffsetGet can process existing fields after changing the mode, I
suppose.
------------------------------------------------------------------------
[2010-11-24 04:13:05] public at grik dot net
Description:
------------
If the object is created and filled by PDOStatement::fetchObject, the
ArrayObject::ARRAY_AS_PROPS is ignored.
Seems like PDO sets the fields values directly around ArrayObject magic
setters, and breaks the ArrayObject native behaviour.
Test script:
---------------
class ArraySlice extends ArrayObject{
function __construct(array $array=array()){
parent::__construct($array,ArrayObject::ARRAY_AS_PROPS);
}
}
$PDO = new PDO($dsn, $user, $password);
$a = $PDO->query('select id from links')->fetchObject('ArraySlice');
echo $a->id; //works
echo $a['id'];//Notice occured ... Undefined index: id
Expected result:
----------------
value of the id field
Actual result:
--------------
Notice
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=53394&edit=1