【发布时间】:2019-08-23 16:01:43
【问题描述】:
我目前正在优化学说查询,并注意到学说会对其在工作单元 (UOW) 中已获得的数据生成额外的查询。
然而,当我尝试将子对象添加/关联到它的父对象时,Doctrine 正在查询以获取所有子对象(它们已经在 UOW 中)
这就是我的做法:
//Getting (root) Salle of the view
$salle=$salleRepository->findBy(array('isAccueil'=>true));
//Get '$salle' children
$salles=$salleRepository->findBy(array('salle'=>$salle));
//For each children of each `Salle` in $salle
foreach($salleRepository->findBy(array('salle'=>$salles)) as $child) {
//Go through each parent
foreach($salles as $parent) {
//If parent id match child parent id
if($parent->getId() == $child->getSalle()->getId()) {
//Doctrine extra query happen here
//Query for current parent's children
$parent->addSalle($child);
}
}
}
正如代码注释中所写,Doctrine 查询当前父母的孩子。
但是这些孩子已经存在于 UOW 中,我确实在第一个 foreach 上查询了他们。
有没有办法将孩子与父母重新关联,这样 Doctrine 就不会做额外的查询?
【问题讨论】: