Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO FETCH_CLASS and Namespacing issue

Tags:

php

pdo

I am trying to use PDO::FETCH_CLASS on an object. I am using namespacing and just entering:

$result = $query->fetchAll(\PDO::FETCH_CLASS, 'Product');

or

$result = $query->fetchAll(\PDO::FETCH_CLASS, '\Product');

results in PHP looking for Product.php in the root of the app.

I can successfully instantiate a new Product though using:

 $product = new Product();

So I know my name spacing is working.

Is this not possible? Or do I need to instantiate a Product first then populate it after from the query?

like image 868
BobFlemming Avatar asked Oct 27 '11 10:10

BobFlemming


1 Answers

I'd suspect PDO does not look up aliased class names or resolve the current namespace. So you have to pass it explicitely:

= $query->fetchAll(\PDO::FETCH_CLASS,  __NAMESPACE__ . '\\Product');

Exactness nitpick: Note that while a single backslash does work, in single quotes it is intended to escape literal single quotes and itself.

like image 65
mario Avatar answered Sep 22 '22 10:09

mario