简体   繁体   中英

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.

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.

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()
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM