[DBAL-196] The function fetchAll(PDO::FETCH_ALL, 'MyClass') doesn't create object of MyClass Created: 06/Jan/12  Updated: 21/Jan/12  Resolved: 21/Jan/12

Status: Resolved
Project: Doctrine DBAL
Component/s: None
Affects Version/s: None
Fix Version/s: 2.2
Security Level: All

Type: Bug Priority: Major
Reporter: Antoine Froger Assignee: Fabio B. Silva
Resolution: Fixed Votes: 0
Labels: None


 Description   

The code below doesn't create objects of MyClass when it has to.
Instead it create StdClass objects

$stmt = $db->prepare($sql);
$stmt->execute();

$results = $stmt->fetchAll(
    \PDO::FETCH_CLASS,
    'MyClass'
);

The problem comes from the file Doctrine/DBAL/Statement.php

    /**
     * Returns an array containing all of the result set rows.
     * 
     * @param integer $fetchStyle
     * @param integer $columnIndex
     * @return array An array containing all of the remaining rows in the result set.
     */
    public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $columnIndex = 0)
    {
	if ($columnIndex != 0) {
            return $this->_stmt->fetchAll($fetchStyle, $columnIndex);
        }
        return $this->_stmt->fetchAll($fetchStyle);
    }

The line

if ($columnIndex != 0) {

must be replace by

if ($columnIndex !== 0) {

because the parameter $columnIndex is not always an integer and could be a name of a class : see the example n°4 in php doc http://www.php.net/manual/en/pdostatement.fetchall.php



 Comments   
Comment by Benjamin Eberlei [ 21/Jan/12 ]

Fixed and merged into 2.2

Generated at Fri Dec 19 06:38:33 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.