Doctrine2空集合创建了不必要的查询

时间:2021-09-05 00:01:36

I have an Doctrine2 entity with an images collection. Here is my query:

我有一个带有图像集的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();

When I call $result->getImages() and the images collection is empty there is an extra call to the database. How can I prevent this?

当我调用$ result-> getImages()并且images集合为空时,会对数据库进行额外调用。我怎么能阻止这个?

1 个解决方案

#1


3  

I had the same problem with native query and I solved it by marking the freshly mapped collection as initialized .

我遇到了与本机查询相同的问题,我通过将新映射的集合标记为已初始化来解决它。

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

Doctrine don't call anymore the DB to check if there is children. Huge optimisation !

Doctrine不再调用数据库来检查是否有孩子。巨大的优化!

#1


3  

I had the same problem with native query and I solved it by marking the freshly mapped collection as initialized .

我遇到了与本机查询相同的问题,我通过将新映射的集合标记为已初始化来解决它。

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

Doctrine don't call anymore the DB to check if there is children. Huge optimisation !

Doctrine不再调用数据库来检查是否有孩子。巨大的优化!