Closed
Description
In a custom FormType, I have:
<?php
class TypePuissanceFiscaleType extends AbstractType
{
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$query_builder = function(Options $options) {
$typeVehicule = $options['type_vehicule_id'];
return function(EntityRepository $repository) use ($typeVehicule) {
return $repository->createQueryBuilder('tpf')
->where('tpf.typeVehicule = :typeVehicule')
->setParameter('typeVehicule', $typeVehicule);
};
};
$resolver->setDefaults(array(
'data_class' => '<class>',
'class' => '<Bundle>:TypePuissanceFiscale',
'type_vehicule_id' => null,
'query_builder' => $query_builder
));
}
public function getParent()
{
return 'entity';
}
public function getName()
{
return 'typepuissancefiscale';
}
}
The qb is supposed to return a different value according to the value of $options['type_vehicule_id']
.
<?php
$form->add('first', 'typepuissancefiscale', array('type_vehicule_id' => 1);
$form->add('first', 'typepuissancefiscale', array('type_vehicule_id' => 2);
However due to resolved options being cached by form types, the options would be resolved only once and re-used for the second form. This means that both forms would use the same query builder which is different from what I expect.
@bschussek
- It is very dangerous and should be caught at least in dev mode (if it implies overhead) rather than silently trigerring a wrong behavior.
- Do you have any idea of how to fix this (not on how to avoid using the options to pass parameters).
/cc @yohannpoli