I have the following entity relations:
Use the constructor in AddressType, its works for me..
CustomerType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
...
->add('addresss', 'collection', array(
'label' => 'customer.address',
'type' => new AddressType($your_variable),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
))
;
}
AddressType:
private $your_variable;
public function __construct($variable)
{
$this->your_variable= $variable;
}
...
public function buildForm(FormBuilderInterface $builder, array $options){
$your_variable = $this->your_variable;
'query_builder' => function(CityRepository $cr) use ($your_variable) {
return $cr->getCityQB($your_variable);
},
}
in symfony3 :
$builder->add('example', CollectionType::class, array(
'entry_type' => ExampleType::class,
'entry_options' => array(
'my_custom_option' => true),
));
I think you could use the 'options' option of the collection type. It's better than using the constructor in case you want to reuse the form elsewhere.
Symfony Form Reference: Collection Type
But remember to define the variable in your setDefaultOptions
method. (Both forms must have it)