1

我有两个实体,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;
4

1 回答 1

6

使用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();

或者

请参阅此http://docs.doctrine-project.org/en/2.1/tutorials/extra-lazy-associations.html

于 2013-03-31T12:37:02.260 回答