Symfony:分页与内部连接 [英] Symfony : Pagination with inner join
问题描述
这是我的查询:
$ query = $ this-> getEntityManager()
- > createQueryBuilder();
$ query-> setFirstResult(($ page - 1)* $ maxperpage);
$ query-> setMaxResults($ maxperpage);
$ query-> select('d')
- > from('DemandeBundle:Declaration','d')
- > orderBy('d。 id','ASC')
- > innerJoin('ContactBundle:Contact','c','WITH','d.contact = c')
- > where(' struct_id =:struct_id')
- > setParameter('structure_id',$ structId)
- > getQuery()
- > getResult();
返回新的Paginator($ query,true);
当我没有使用innerJoin但是我需要使用它才能显示请求关于我的用户。
使用innerJoin我有这样的错误:
在渲染模板期间抛出一个异常
(不能计数选择两个FROM组件的查询,不能区分)
DemandeBundle:Demande:listing_demande.html.twig第25行
如何避免使用其他捆绑包或其他任何东西。 b
$ b
希望你会明白我的家伙。
你的声明
以某种方式与联系
相关联?
在联系人中
ManyToOne
关系远远好于/ code>指向声明
。这样,它将会起作用,因为您不会有两个FROM组件,而是单个。
然后,修改查询:
- > innerJoin('d.contant','c')
完整的查询应如下所示:
$ query-> select('d')
- > from('DemandeBundle:Declaration','d')
- > orderBy('d.id','ASC')
- > innerJoin('d.contact','c')//< - 这个线是CRITICAL
- >其中('c.structure_id =:structure_id')
- > setParameter('structure_id',$ structureId)
- > getQuery()
- > getResult();
I need to implement a pagination. Seemingly, Doctrine doesn't support some jointures.
Here is my query :
$query = $this->getEntityManager()
->createQueryBuilder();
$query->setFirstResult(($page - 1) * $maxperpage);
$query->setMaxResults($maxperpage);
$query->select('d')
->from('DemandeBundle:Declaration', 'd')
->orderBy('d.id', 'ASC')
->innerJoin('ContactBundle:Contact', 'c', 'WITH', 'd.contact = c')
->where('c.structure_id = :structure_id')
->setParameter('structure_id', $structureId)
->getQuery()
->getResult();
return new Paginator($query, true);
It's working fine when I am not using innerJoin but I need to use it so as to display only requests regarding to my user.
Using innerJoin I got that kind of error :
"An exception has been thrown during the rendering of a template
("Cannot count query which selects two FROM components, cannot make distinction") in
DemandeBundle:Demande:listing_demande.html.twig at line 25"
How can I circumvent this problem without using another bundle or whatever.
Hope you will understand me guy.
Is your Declaration
somehow related to Contact
?
It's far better for you to have ManyToOne
relation in Contact
that points to Declaration
. That way, it will work since you won't have two FROM components, but single one instead.
Then, modify the query to do:
->innerJoin('d.contant', 'c')
The full query should look like this:
$query->select('d')
->from('DemandeBundle:Declaration', 'd')
->orderBy('d.id', 'ASC')
->innerJoin('d.contact', 'c') // <-- THIS LINE IS CRITICAL
->where('c.structure_id = :structure_id')
->setParameter('structure_id', $structureId)
->getQuery()
->getResult();
这篇关于Symfony:分页与内部连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!