Search code examples
phpobjectpdo

How to force PDOStatement->fetchAll to return array of objects?


I am writing my own simply ORM using PDO. My question is if you can force PDOStatement::fetchAll() method to return array of objects of stdClass? For example:

$result = $q->fetch_all(/* some magic here */);
print_r($result);

Should print something like:

Array
(
    [0] => stdClass Object
        (
            [NAME] => pear
            [COLOUR] => green
        )

    [1] => stdClass Object
        (
            [NAME] => watermelon
            [COLOUR] => pink
        )

)

Is this posible? NAME and COLOUR are of course names of columns. I read documentation but I didn't find anything interesting.


Solution

  • Use $result = $q->fetchAll(PDO::FETCH_OBJ);