简体   繁体   English

Symfony主义查询生成器arraycollection中的最后一个

[英]Symfony Doctrine Query Builder Where last in arraycollection

I want to use symfony's query builder and add a where to the last item in an array collection 我想使用symfony的查询生成器,并在数组集合的最后一项中添加一个位置

$query = $em->getRepository('RlBookingsBundle:Booking')->createQueryBuilder('b')
        ->select('b, v, c, ca, q')
        ->leftJoin('b.vehicle', 'v')
        ->leftJoin('b.customer', 'c')
        ->leftJoin('c.address', 'ca')
        ->leftJoin('b.quote', 'q')
        ->leftJoin('b.history', 'h') //This is an array collection
        ->orderBy('b.edited', 'DESC')
    ;

I want to use only the latest value from history as it is a log but only the most recent entry is valid 我只想使用历史记录中的最新值,因为它是日志,但是只有最新条目才有效

->where('h.status IN (:status)')
  ->setParameter('status', [7]);

Will return all results with h.status = 7 but I would like it to only query the most recent result. 将返回h.status = 7的所有结果,但我希望它仅查询最新结果。 Is there anyway to do this? 反正有这样做吗?

I tried a groupby on the history field but this seems to groupby with data from the first entry, even if I add an orderby to it. 我在“历史记录”字段上尝试了一个groupby,但这似乎是将第一个条目中的数据进行了groupby,即使我向其中添加了orderby。

If the results you get are already ok, but you only want the first, you could just use 如果您获得的结果已经可以,但是您只想要第一个,则可以使用

...
->setMaxResults(1)
...

If you want to order by history ID desc, you may want to add another orderBy clause before the existing one 如果要按历史记录ID desc进行排序,则可能要在现有的orderBy子句之前添加另一个

...
->orderBy('h.id', 'DESC')
->orderBy('b.edited', 'DESC')
...

If it's more complex than that, I strongly suggest you perform a separate query to get the desired record(s) from history, and THEN use it as a filter, instead of the leftJoin . 如果比这更复杂,我强烈建议您执行一个单独的查询以从历史记录中获取所需的记录,然后将其用作过滤器,而不是leftJoin

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

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