php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53394 ARRAY_AS_PROPS flag is ignored when used with PDOStatement::fetchObject
Submitted: 2010-11-24 04:13 UTC Modified: 2011-11-16 14:24 UTC
Votes:3
Avg. Score:3.3 ± 0.5
Reproduced:2 of 3 (66.7%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: public at grik dot net Assigned:
Status: Open Package: Documentation problem
PHP Version: 5.3.3 OS:
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2010-11-24 04:13 UTC] 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 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-24 13:46 UTC] 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 13:54 UTC] 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 15:25 UTC] tony2001@php.net
-Status: Open +Status: Feedback
 [2010-11-24 15:25 UTC] 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 15:27 UTC] tony2001@php.net
-Status: Feedback +Status: Assigned -Assigned To: +Assigned To: felipe
 [2010-11-24 15:27 UTC] tony2001@php.net
Felipe, could you check this out plz?
 [2010-11-24 22:16 UTC] felipe@php.net
-Status: Assigned +Status: Bogus
 [2010-11-24 22:16 UTC] felipe@php.net
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.
 [2010-11-24 23:32 UTC] public at grik dot net
It's not bogus. It is a documentation issue at least.
I guess, it needs to be described somewhere beyond the bugtracker.

all I see in docs is:
PDO::FETCH_PROPS_LATE ( integer ) 
Available since PHP 5.2.0
 [2010-11-24 23:49 UTC] felipe@php.net
-Status: Bogus +Status: To be documented -Assigned To: felipe +Assigned To:
 [2010-11-24 23:49 UTC] felipe@php.net
Right, it needs be documented.
 [2011-11-16 14:24 UTC] felipe@php.net
-Package: SPL related +Package: Documentation problem
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 14:02:33 2014 UTC