Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get association foreign key IDs in Doctrine 2 without loading the associated object?

Hi I'm having trouble with what I thought would be an easy task.

I am retrieving a post from the database. The Post entity has a field createdBy which is associated to a User entity.

What I would like to do is load Post and User with two separate queries (no join). That means I need to have access to the created_by foreign key integer on the $post object. Doctrine does not seem to expose that at all. A var_dump of post shows createdBy => null. If I join the user on directly in the post query createdBy => User object. Is there no way to get the created_by foreign key integer from post so I can query for the user?

Thanks

like image 214
Marc Avatar asked Jul 26 '11 21:07

Marc


1 Answers

Use this on your query:

$q->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true);
$q->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);

Hydratation is disabled, so you have your result as an array.

like image 52
Maxence Avatar answered Nov 14 '22 04:11

Maxence