Matheus Gontijo
Matheus Gontijo

Reputation: 1307

How to retrieve foreign keys using Doctrine 2 Query Builder when the result is hydrated as an array?

$posts = $qb
    ->select('p')
    ->from('MyBundle\Entity\Post', 'p')
    ->getQuery()
    ->getArrayResult();

The query above will return something like this (+99 columns)

id | title | url                    | .... many columns here .....  | created_at
---|--------------------------------|-------------------------------|--------------------
 1 | hello | http://www.google.com/ |  |  - | -  | -  | -  | -  | - | 2017-01-01 00:00:00
 2 | world | http://www.yahoo.com/  |  |  - | -  | -  | -  | -  | - | 2017-01-01 00:00:00

However, this table has an FK (user_id) which refers to the table "user". The problem is this: the query doesn't bring the FK columns into the query result. I tested and when the result is hydrated as an array (getArrayResult) it ignores FK's on result, when the result is hydrated as an object (getResult) it brings FK's on result. Okay, I read this helpful post (http://shout.setfive.com/2015/01/31/including-foreign-keys-in-doctrine2-array-results/) and figured out an way to retrieve the FK as the code below shows (not sure if this is the appropriate way, but at works)

$posts = $qb
    ->select('p')
    ->from('MyBundle\Entity\Post', 'p')
    ->getQuery()
    ->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true)
    ->getArrayResult();

Nonetheless I face another problem which I couldn't solve so far. As I said this table has +99 columns. However, I don't want all of them. What I do want are only 3 of the +99. The code below work properly until...

$posts = $qb
    ->select(array('p.id', 'p.url'))
    ->from('MyBundle\Entity\Post', 'p')
    ->getQuery()
    ->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true)
    ->getArrayResult();

...until I add the "p.user" on select, which stands for the FK (refering to the table user, column id).

$posts = $qb
    ->select(array('p.id', 'p.url', 'p.user'))
    ->from('MyBundle\Entity\Post', 'p')
    ->getQuery()
    ->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true)
    ->getArrayResult();

That's what I get:

[Doctrine\ORM\Query\QueryException]                                                                                                              
[Semantical Error] line 0, col 10 near 'user FROM MyBundle\Entity\Post': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

Upvotes: 3

Views: 2559

Answers (2)

LBA
LBA

Reputation: 4089

Try something like

->select(array('p.id', 'p.name', 'IDENTITY(p.user)'))

this will return the identifier / foreign key of your user. I never worked with the select(array()) notation so my example might not work out of the box. but IDENTITY(entity.association)is what you're looking for.

Upvotes: 4

Rendy Eko Prastiyo
Rendy Eko Prastiyo

Reputation: 1098

Here it is:

$posts = $qb
    ->select(array('p.id', 'p.url', 'u.id')) // u.id is your user id, change if your user id is different
    ->from('MyBundle\Entity\Post', 'p')
    ->leftJoin('MyBundle\Entity\User', 'u')
    ->getQuery()
    ->getArrayResult()
;

Upvotes: 0

Related Questions