2013-03-31 72 views
1

我有兩個實體,Order和Orderline具有一對多的關係。我有一個列表,我只需要訂單中的數據,但Doctrine會爲每個訂單的訂單項查詢。我沒有在查詢中加入Orderlines。如何防止學說查詢命令行?如何防止在Symfony2中加載相關實體?

查詢在控制器中使用:

$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(); 

一到多的順序類註釋:

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

多到一個註釋的訂單項目類:

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

回答

5

使用Doctrine\ORM\Query::HINT_FORCE_PARTIAL_LOAD

0暗示教條
$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

+0

謝謝! Hnting解決了我的問題。 – user1995453