我有一個產品實體和一個商店實體。學說 - 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實體字段上)?
我最初以爲只有第一個例子就是工作...