我的Symfony(3.3)表单EntityType显示为选择输入并列出了我们在数据库中拥有的所有客户端。客户端实体与使用懒惰模式的其他几个实体关联。
当选择框被渲染时,发出204个数据库查询。我怀疑表单组件根据每个查询结果调用setter,导致加载许多额外的数据库查询。我们可以将关联映射设置为“EAGER”,或者在窗体的querybuilder选项中使用join('...') - > addSelect('...')方法强制数据成为结果的一部分,但当涉及多个实体时,水合过程仍然昂贵。你可以看到,我试图使用Doctrine Query HINT,希望它能解决问题,但它没有改变任何东西。Symfony - 在EntityType查询生成器的水合过程中丢弃关联字段
那么,这样的用例有什么办法呢?
我应该怎么做才能得到我需要填充下拉输入的字段?
这里是我试过到目前为止:
$builder->add('parent', EntityType::class, [
'class' => Client::class,
,'required' => false
,'multiple' => false
,'query_builder' => function (EntityRepository $er) {
$qb = $er ->createQueryBuilder('c')
// All I want doctrine to fetch are the following fields
->select('PARTIAL c.{id,uuid,name,shortName}');
// I expected this flag to help but it does not change the total amount of queries executed
$qb->getQuery()->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
return $qb;
}
])…
谢谢。