Symfony2和Doctrine2 - 具有关系实体的QueryBuilder [英] Symfony2 and Doctrine2 - QueryBuilder with relational entities

查看:149
本文介绍了Symfony2和Doctrine2 - 具有关系实体的QueryBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Symfony2项目中,我有一个这样的查询:

  $ paperQB = $ this-> createQueryBuilder p')
- > select('p')
- >其中(p.title LIKE:q OR p.keywords LIKE:q OR p.abstract LIKE:q OR p.id LIKE:q)
- > setFirstResult($ first_result)
- > setMaxResults($ papers_per_page)
- > orderBy($ sort_by_culumn,$ sort_by_order)
- > ; setParameter('q','%'。$ q。'%');

一切都很好,但在我的论文实体中,我与该部分有很多关系实体。所以,我也想得到:



OR p.section.name LIKE:q



怎么可能,我应该使用加入来做到这一点吗?查询bilder不知道如何在控制器中使用这些关系: $ p-> getSection() - > getName()或在twig p.section.name



非常感谢。



编辑:



目前我所做的是:

 code> $ paperQB = $ this-> createQueryBuilder('p')
- > select('p')
- >其中(p.title LIKE:q OR p关键字LIKE:q OR p.abstract LIKE:q OR p.id LIKE:q OR s.name LIKE:q OR c.name_full LIKE:q)
- > leftJoin('p.conference' 'c')
- > leftJoin('p.section','s')
- > setFirstResult($ first_result)
- > setMaxResults($ papers_per_page)
- > orderBy($ sort_by_culumn,$ sort_by_order)
- > setParameter('q','%'。$ q。'%');

但是,为什么Query构建器不使用实体及其关系?

解决方案

出于性能原因,默认行为不加载相关实体的字段,它只提供与OBJECT水合化(即默认水合模式)的那些关系的代理类。 / p>

您必须按照您的要求陈述JOIN。



编辑:您可以从Controller或 p.section.name $ p-> getSection() - > getName() >从Twig模板中,没有在查询中声明JOIN(使用默认的QueryBuilder),Doctrine此时请求丢失关系的信息。如果您在循环中执行此操作,就像在列表显示中那样可能会非常沉重...所以尝试从第一个请求的关系中加载所需的内容);


In my Symfony2 project, I have a query like this:

$paperQB = $this->createQueryBuilder( 'p' )
        ->select('p')
        ->where("p.title LIKE :q OR p.keywords LIKE :q OR p.abstract LIKE :q OR p.id LIKE :q")
        ->setFirstResult( $first_result )
        ->setMaxResults( $papers_per_page )
        ->orderBy($sort_by_culumn, $sort_by_order)
        ->setParameter('q', '%'.$q.'%');

Everything is fine there, but In my paper entity, I have a many to one relation with the section entity. So, I would like to get also:

"OR p.section.name LIKE :q"

How is this possible, should I use a join in order to do that? The query bilder does not know how to use those relations as in the controller: $p->getSection()->getName() or in the twig p.section.name?

Thanks a lot.

EDIT:

What I did for the moment:

$paperQB = $this->createQueryBuilder( 'p' )
        ->select('p')
        ->where("p.title LIKE :q OR p.keywords LIKE :q OR p.abstract LIKE :q OR p.id LIKE :q OR s.name LIKE :q OR c.name_full LIKE :q")
        ->leftJoin('p.conference', 'c')
        ->leftJoin('p.section', 's')
        ->setFirstResult( $first_result )
        ->setMaxResults( $papers_per_page )
        ->orderBy($sort_by_culumn, $sort_by_order)
        ->setParameter('q', '%'.$q.'%');

But why Query builder does not use entities and their relations ?

解决方案

Default behaviour does not load related entities' fields for performance reasons, it only provides a proxy class of those relations with OBJECT hydratation (that is default hydratation mode).

You have to state the JOIN as you well did.

EDIT: When you do $p->getSection()->getName() from a Controller or p.section.name from a Twig template, without having stated the JOIN in the query (with default QueryBuilder), Doctrine requests missing informations of the relation at this time. That could be very heavy if you do that in a loop, like in a list display... So try to load what you need from relations at first request ;)

这篇关于Symfony2和Doctrine2 - 具有关系实体的QueryBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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