如何防止在Symfony2中加载相关实体? [英] How can I prevent loading of related entities in Symfony2?
本文介绍了如何防止在Symfony2中加载相关实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个具有一对多关系的实体,Order和Orderline。我有一个清单,其中只需要来自订单的数据,但是Doctrine会查询每个订单的订单行。我没有加入查询中的订单行。
I have two entities, Order and Orderline with one-to-many relationship. I have a listing where I need only data from orders, but Doctrine makes query for orderlines for each order. I haven't joined Orderlines in the query. How can prevent Doctrine for querying the orderlines?
控制器中使用的查询:
$query = $em->createQuery("SELECT o FROM SomeBundle:Order o JOIN o.shop s JOIN s.user u WHERE u.id = :user_id AND o.status = :status")
->setParameter('user_id', $user->getId())
->setParameter('status', 'paid');
$orders = $query->getResult();
Order类中的一对多注释:
One-to-many annotation in Order class:
/**
* @ORM\OneToMany(targetEntity="Orderline", mappedBy="order")
*/
protected $orderlines;
Orderline类中的多对一注释:
Many-to-one annotation in Orderline class:
/**
* @ORM\ManyToOne(targetEntity="Order", inversedBy="orderlines")
*/
protected $order;
推荐答案
使用 Doctrine\ORM\Query :: HINT_FORCE_PARTIAL_LOAD
$query = $em->createQuery("SELECT partial o.{id} FROM SomeBundle:Order o JOIN o.shop s JOIN s.user u WHERE u.id = :user_id AND o.status = :status")
->setParameter('user_id', $user->getId())
->setParameter('status', 'paid');
$query->setHint(Doctrine\ORM\Query::HINT_FORCE_PARTIAL_LOAD, 1);
$orders = $query->getResult();
OR
查看此 http://docs.doctrine-project.org/en/2.1/ tutorials / extra-lazy-associations.html
这篇关于如何防止在Symfony2中加载相关实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文