I have two entities, A and B. A has a one-to-many relation with B.
I am embedding five B forms inside a A form using the collection
field type. To get that, I'm creating 5 A-B relations in my AController
. What I would like to do is to use a field of each of this B
entities to build its label in the form collection.
So, I have the following code:
//AController
$a = new A();
//Followinf returns an array of 5 B entities
$bs = $this->getDoctrine->getEntityManager()->getRepository('MyBundle:B')->findBy(array(
'field' => 'value',
));
foreach ($bs as $b) {
$a->addB($b);
}
$form = $this->createForm(new AType(), $a);
return array(
'a' => $a,
'form' => $form->createView(),
);
//AType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a_field')
->add('another_field')
->add('bs', 'collection', array(
'type' => new BType(),
'options' => array(
'label' => 'i want to configure it depending on current B data',
)
))
;
}
I have found this related topic:
Symfony form - Access Entity inside child entry Type in a CollectionType
But notice this is different because it access the data in the child form. I want to access children data from the parent form and use it for each child label in the collection.
I know I can access child data using $builder->getData()->getBs();
but I can't figure how to use it later for each child form.
I know as well I can do it in the view, looping through the entities and using the loop index to render manually each collection element, but I would like to do it in the form.
Thanks a lot.