我正在尝试提供> 300个选项的选择
$form->add('products', EntityType::class, array(
'class' => Product::class,
'query_builder' => function (ProductRepository $er) use ($customerId) {
return $er->QBByCustomer($customerId);
},
'choice_label' => 'l.name',
));
QueryBuilder:
public function QBByCustomer($customer = null)
{
return $this->QB()
->addSelect('p.name AS HIDDEN name')
->join('p.customer', 'c')
->join('p.label', 'l')
->where('c.customer = :customer')
->setParameter('customer', $customer)
->addOrderBy('name')
;
}
当我渲染表单时,Doctrine会生成> 300个查询来加载每个相关对象。
有没有办法告诉Doctrine使用我提供的QueryBuilder中的标签,而不是像可选项一样激发查询?
答案 0 :(得分:2)
由于显示的标签是从链接的实体(label
)中提取的,我们必须帮助Doctrine加载它。
以下是QueryBuilder的外观:
'query_builder' => function (ProductRepository $er) use ($customerId) {
$qb = $er->QBByCustomer($customerId);
$qb->addSelect('partial p.{id}');
$qb->addSelect('partial l.{id, name}');
return $qb;
},
'choice_label' => 'label.name',