Symfony2联接查询 [英] Symfony2 join query

查看:65
本文介绍了Symfony2联接查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视频表,并且在该表中我有一个字段注释,其中包含其他表中的注释ID,现在我使用联接查询在一个查询中得到它,但是如何获取该注释?

I have a table of videos and in that table I have field comment which contains id of comment in other table, now I used join query to get that in one query, but how do I get that comment?

这是我的代码:

$Actions = $this->EntityManager()->getRepository('AppBundle:Video')
                                    ->createQueryBuilder('V')
                                    ->join('AppBundle:VideoComment', 'VC')
                                    ->where('V.videoId = :VideoID')
                                    ->andWhere('VC.videoId = :VideoID')
                                    ->setParameter('VideoID', $VideoID)
                                    ->getQuery()
                                    ->getResult();

我如何从该加入实体获得实际评论?

How do I get the actual comment from that joined entity?

推荐答案

您可以做@cezar前面说的,但做一点改动:您必须定义字段以从注释表中检索相关条目。
因此,您的查询可能像这样:

You can do what @cezar said earlier but with one little change: you have to define field to retrieve related entries from comments table. So, your query might look like this:

$em = $this->get('doctrine.orm.entity_manager');
$videos = $em->createQuery('select v 
                            from YourBundle:Video v 
                            left join YourBundle:Comment c 
                            where v.comment = c.id')
             ->getResult();

,也可以使用查询生成器执行类似的操作:

or you can do the similar stuff using query builder:

$videos = $em->createQueryBuilder('v')
             ->add('select', 'v, c')
             ->add('from', 'YourBundle:Video v')
             ->leftJoin('YourBundle:Comment', 'c')
             ->where('v.comment = c.id')
             ... // some other conditions if you need
             ->getQuery()
             ->getResult();

我所描述的两个案例都说明该Video and Comment实体可能不在正式关系中(我的意思是他们的关系可能不会在您的学说/ orm文件中描述。)

Both cases I described account for that Video and Comment entity might not be in formal relations (I mean their relations might not be described in your doctrine/orm file).

这篇关于Symfony2联接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆