php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #53394 Insufficient docs regarding PDOStatement::fetch(Object) and constructors
Submitted: 2010-11-24 04:13 UTC Modified: 2016-06-29 11:04 UTC
Votes:5
Avg. Score:4.0 ± 0.9
Reproduced:4 of 5 (80.0%)
Same Version:2 (50.0%)
Same OS:1 (25.0%)
From: public at grik dot net Assigned: cmb (profile)
Status: Closed Package: PDO related
PHP Version: 5.3.3 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
37 + 9 = ?
Subscribe to this entry?

 
 [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
 [2016-06-29 10:58 UTC] cmb@php.net
-Summary: ARRAY_AS_PROPS flag is ignored when used with PDOStatement::fetchObject +Summary: Insufficient docs regarding PDOStatement::fetch(Object) and constructors -Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem -Package: Documentation problem +Package: PDO related -Assigned To: +Assigned To: cmb
 [2016-06-29 11:03 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&revision=339520
Log: Fix #53394: Insufficient docs regarding PDOStatement::fetch(Object) and constructors
 [2016-06-29 11:04 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2016-06-29 11:04 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2020-02-07 06:07 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=e440c79af15cf829549736b2a292b5f8f6887142
Log: Fix #53394: Insufficient docs regarding PDOStatement::fetch(Object) and constructors
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC