Reputation: 46208
I'm trying to provide a selection with >300 choices
$form->add('products', EntityType::class, array(
'class' => Product::class,
'query_builder' => function (ProductRepository $er) use ($customerId) {
return $er->QBByCustomer($customerId);
},
'choice_label' => 'l.name',
));
The 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')
;
}
When I render the form, Doctrine generates >300 queries to load each related objects.
Is there a way to tell Doctrine to use the labels from the QueryBuilder I gave, instead of firing as much query as selectable items ?
Upvotes: 1
Views: 258
Reputation: 46208
Since the displayed label is fetched from a linked entity (label
), we have to help Doctrine to load it.
Here is how the QueryBuilder should look like:
'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',
Upvotes: 2