簡體   English   中英

Doctrine2空集合創建不必要的查詢

[英]Doctrine2 empty collection creates unnecessary query

我有一個帶有images集的Doctrine2實體。 這是我的查詢:

$qb = $this->createQueryBuilder('page');
    $qb->select('page, image');
    $qb->where('page.id = ?1')
       ->leftJoin('page.images', 'image')
       ->setParameter(1, $id);

$result = $qb->getQuery()->getOneOrNullResult();

當我調用$result->getImages()並且images集合為空時,會對數據庫進行額外調用。 我怎么能阻止這個?

我對本機查詢也遇到了同樣的問題,我通過將新映射的集合標記為Initialized解決了它。

if($result->getImages() instanceof PersistentCollection)
    $result->getImages()->setInitialized(true);
}

教義不再呼叫數據庫檢查是否有孩子。 巨大的優化!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM