php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #44407 When using FETCH_CLASS|CLASS_TYPE 'MyClass' an array of stdClass returned
Submitted: 2008-03-11 16:00 UTC Modified: 2008-11-07 11:05 UTC
Votes:4
Avg. Score:4.5 ± 0.9
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:0 (0.0%)
From: robert dot allen at zircote dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.2.5 OS: Windows XP
Private report: No CVE-ID: None
 [2008-03-11 16:00 UTC] 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 (.....

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-12 16:41 UTC] 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-10-14 13:50 UTC] 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-11-06 09:56 UTC] sng at crackhouse dot dk
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.
 [2008-11-07 11:05 UTC] vrana@php.net
To use 'MyClass', just pass PDO::FETCH_CLASS. It's clearly written in documentation.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 13:01:32 2024 UTC