我有兩個實體共享一個創建類表繼承的抽象類。我可以通過使用抽象類的存儲庫來查詢實體,並獲取擴展抽象類作爲結果的所有實體。加入關係關於Doctrine中的實體子類
$qb = $this->createQueryBuilder('c')
->where('c.featured = true')
->orderBy('c.sticky', 'DESC')
->addOrderBy('c.weight', 'ASC')
->setFirstResult($offset)
->setMaxResults($limit);
// Returns 8 results, results in 34 queries
子類包含到其他實體多對多的關係,所以如果我以這種方式進行查詢,這種關係導致額外的查詢,因爲他們沒有被加入。你如何查詢擴展抽象類並加入其列的實體?我試圖用左連接從語句中添加多個,但查詢返回的結果少於預期的8個。該查詢生成器看起來是這樣的:
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select(array(
'a', 'artist', 'country',
't', 'artwork', 'user'))
->from('AcmeArtworkBundle:Artwork', 'a')
->from('AcmeTourBundle:Tour', 't')
->leftJoin('a.artist', 'artist')
->leftJoin('a.country', 'country')
->leftJoin('t.artwork', 'artwork')
->leftJoin('t.user', 'user')
->where('a.featured = true')
->andWhere('t.featured = true')
->orderBy('a.sticky', 'DESC')
->addOrderBy('t.sticky', 'DESC')
->addOrderBy('a.weight', 'ASC')
->addOrderBy('t.weight', 'ASC')
->setFirstResult($offset)
->setMaxResults($limit);
// 5 results :-(
急切地提取並不總是正確的解決方案,因爲ORM並不總是能夠將所有必要的連接添加到用於提取對象的SQL中。 – Ocramius
是的,這是重要的一點。這不是一種適合所有解決方案的解決方案,它適用於我的情況。 – ornj