2013-04-01 49 views
1

我有一個產品實體和一個商店實體。學說 - QueryBuilder,用where參數選擇查詢:實體還是ids?

一家商店可以有0到n個產品,一個產品只能在一個商店中。

產品實體表因此通過shop_id表字段引用Shop實體。

當使用學說查詢生成器對於給定店的產品查詢,我們可以這樣做:

$products = $this->getDoctrine()->getRepository('MyBundle:Product') 
       ->createQueryBuilder('p') 
       ->where('p.shop = :shop') 
       ->setParameter('shop', $shop) // here we pass a shop object 
       ->getQuery()->getResult(); 

或本:

$products = $this->getDoctrine()->getRepository('MyBundle:Product') 
       ->createQueryBuilder('p') 
       ->where('p.shop = :shopId') 
       ->setParameter('shopId', $shopId) // we pass directly the shop id 
       ->getQuery()->getResult(); 

而且似乎都工作...我因此我想知道:在這種情況下,我們總是可以直接傳遞實體id,而不是實體實例(即:在引用另一個實體的doctrine實體字段上)?

我最初以爲只有第一個例子就是工作...

回答

0

我可能是錯的,但我認爲,如果傳遞的,而不是身份證號碼對象實例,學說會自動調用$instance->getId()使你的兩個查詢相同時翻譯成SQL(甚至DQL)。