Hi,
I have a one to many relation. A Person have many cars and a car
belongs only to one person. So, when i choose a person from a select
box, I want to display only the cars that this person have.
this is my code :
$factory = $builder->getFormFactory();
$refresh = function ($form, $cars) use ($factory) {
$form->add($factory->createNamed('entity','person', null,
array(
'class' => 'Tritux\\AdminBundle\\Entity\\Person',
'query_builder' => function ($repository) use ($cars) {
$qb = $repository
->createQueryBuilder('person')
->where('person.cars = :cars')
->setParameter('cars',$cars)
;
return $qb;
}
)));
};
}
$builder->addEventListener(FormEvents::SET_DATA,
function (DataEvent $event) use ($refresh) {
$form = $event->getForm();
$data = $event->getData();
if($data == null)
return;
else
$refresh($form, $data->getCars);
});
$builder->addEventListener(FormEvents::PRE_BIND, function (DataEvent
$event) use ($refresh) {
$form = $event->getForm();
$data = $event->getData();
if(array_key_exists('cars', $data)) {
$refresh($form, $data['cars']);
}
});
But, it doesn't work. I want to know if it's the good way. Or there is
another better way to do this !
Thank you !
--
If you want to report a vulnerability issue on symfony, please send it to
security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en