#44407 [Com]: When using FETCH_CLASS|CLASS_TYPE 'MyClass' an array of stdClass returned
| From: | sng at crackhouse dot dk | Date: | Thu, 06 Nov 2008 09:56:44 +0000 |
| Subject: | #44407 [Com]: When using FETCH_CLASS|CLASS_TYPE 'MyClass' an array of stdClass returned | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-1399@lists.php.net to get a copy of this message | ||
ID: 44407
Comment by: sng at crackhouse dot dk
Reported By: robert dot allen at zircote dot com
Status: Open
Bug Type: Documentation problem
Operating System: Windows XP
PHP Version: 5.2.5
New Comment:
Using the suggested approach from comment #14 would not solve the
problem when passing along an array of constructor arguments unless the
specified class has a constructor. Using stdClass for instance, results
in PHP Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY000]: General error: user-supplied class does not have a
constructor, use NULL for the ctor_params parameter, or simply omit it'.
The number of arguments taken by the stand-in class' constructor doesn't
have to be equal to the class returned by the first column of the query.
Previous Comments:
------------------------------------------------------------------------
[2008-10-14 13:50:19] felipe@php.net
Just use PDO::FETCH_CLASS, the flag PDO::FETCH_CLASSTYPE does it works
differently.
As is mentioned in the source:
"fetch class gets its class name from 1st column".
Lets test it!
mysql> create table testz (name varchar(200));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into testz values ('myclass');
Query OK, 1 row affected (0.00 sec)
mysql> insert into testz values ('myclass2');
Query OK, 1 row affected (0.00 sec)
class myclass { }
class myclass2 { }
$stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE,
'myclass');
$stmt->execute();
$all = $stmt->fetchAll();
array(2) {
[0]=>
object(myclass)#3 (0) {
}
[1]=>
object(myclass2)#4 (0) {
}
}
Though currently the third parameter to setFetchMode is mandatory when
using PDO::FETCH_CLASS, it seems incorret when using the flag
FETCH_CLASSTYPE, as the class name come from a table column.
And yes, this must be documented.
Thanks. :)
------------------------------------------------------------------------
[2008-05-12 16:41:08] ludmilla dot bogavac at vgregion dot se
Server OS: Solaris 9, Database server: IBM Informix IDS 9.4, Web Server
Apache/2.0.54, PHP: 5.2.5, ODBC-driver: IBM Informix ODBC Driver
-------------------
Reproduce Code:
class MyBaseClass
{
public $par1;
private $par2;
private $par3;
public $par4;
}
class MyClass extends MyBaseClass
{
public $par5;
public $par6;
public function __construct($par1, $par4, $par5, $par6)
{
$this->par1 = $par1;
$this->par2 = $par1;
$this->par3 = $par4;
$this->par4 = $par4;
$this->par5 = $par5;
$this->par6 = $par6;
}
}
$db = new PDO("informix:connection string parameters", "user",
"password");
$stmt = $db->prepare("EXECUTE PROCEDURE mystoredprocedure()");
$stmt->setFetchMode(PDO::FETCH_CLASS,'MyClass');
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_CLASS);
print_r($result);
----------------------
Expected result:
Array ( [0] => MyClass Object (...
------------------------------
Actual result:
Array ( [0] => stdClass Object (...
------------------------------------------------------------------------
[2008-03-11 16:00:06] robert dot allen at zircote dot com
Description:
------------
Windows XP / PHP 5.2.5 / PDO_ODBC
When setFetchMode is called with FETCH_CLASS and the Object type
specified the Object type is returned as stdClass in the array for
fetchAll() however for fetch() returns the expected result of the
defined object.
Reproduce code:
---------------
$stmt->setFetchMode(PDO :: FETCH_CLASS | PDO :: FETCH_CLASSTYPE,
'MyClass');
$stmt->execute();
$all = $stmt->fetchAll();
Expected result:
----------------
return:
Array ( [0] => MyClass Object (.....
Actual result:
--------------
return:
Array ( [0] => stdClass Object (.....
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=44407&edit=1