简体   繁体   English

当结果作为数组水合时,如何使用Doctrine 2查询生成器检索外键?

[英]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) 上面的查询将返回类似(+99列)的信息

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". 但是,此表有一个FK(user_id),它引用表“ user”。 The problem is this: the query doesn't bring the FK columns into the query result. 问题是这样的:查询不会将FK列带入查询结果。 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. 我测试了一下,当结果作为数组水合时(getArrayResult),它将忽略FK的on结果;当结果作为对象水合时(getResult),将FK的结果作为结果。 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) 好的,我阅读了此有用的文章( http://shout.setfive.com/2015/01/31/includes-foreign-keys-in-doctrine2-array-results/ ),并找到了一种将FK作为下面的代码显示(不确定这是否合适,但可以正常工作)

$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. 正如我所说的,此表有+99列。 However, I don't want all of them. 但是,我不想要所有这些。 What I do want are only 3 of the +99 . 我只想要+99中的3个 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). ...直到我在select上添加“ p.user”,它代表FK(指表用户的列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. 我从未使用过select(array())表示法,因此我的示例可能无法立即使用。 but IDENTITY(entity.association) is what you're looking for. 但是IDENTITY(entity.association)是您想要的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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