Jesus M Jesus M - 10 months ago 70
PHP Question

How to embed a form multiple times in Symfony

I have 2 entities: Tarifa and TarifaPeso (this one has to appear 20 times in the Tarifa form).

The relation in Tarifa is:

* @ORM\OneToMany(targetEntity="TarifaPeso", mappedBy="tarifa", cascade={"persist"})
private $pesos;

I have embed the form as Symfony2 doc says and everything is ok, but I do not want to do it dinamically, it has to appear just 20 times. So Tarifa is a form and has to have 20 TarifaPeso form embeded.

Do you know how to do this? Thanks


As you have maintaining a @ORM\OneToMany association, Collection Field Type should solve your purpose. You just have to associate 20 instances of TarifaPeso with Tarifa Entity before generating the form.

See here how to embed the collection form type.

Make sure you have cascade-persist enabled from Tarifa Entity for automatically insert or update TarifaPeso entities.

From my experience with Collection field types. This will be faster and easier in terms of data integrity.