如何防止在Symfony2中加载相关实体?

时间:2013-03-31 12:10:52

标签: symfony doctrine

我有两个实体,Order和Orderline,具有一对多的关系。我有一个列表,我只需要订单中的数据,但是Doctrine会查询每个订单的订单。我没有在查询中加入Orderlines。如何阻止Doctrine查询订单?

控制器中使用的查询:

$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类中的一对多注释:

/**
 * @ORM\OneToMany(targetEntity="Orderline", mappedBy="order")
 */
protected $orderlines;

Orderline类中的多对一注释:

/**
 * @ORM\ManyToOne(targetEntity="Order", inversedBy="orderlines")
 */
protected $order;

1 个答案:

答案 0 :(得分:5)

使用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