Take the 2-minute tour ×

I have an entity with a many-to-many association.
What I'm trying to do is:

  • create the form for the main entity
  • embed the associated entities

I have been able to do both the points above, but now I cannot figure out how to retrieve an associated entity field.

My classes are Gallery and Immagine. Immagine has 3 properties, image, imageName and file.

Inside my GalleryType buildForm function I do this:

->add('immagini', 'entity', array(
  'class' => 'MySiteBundle:Immagine',
  'property' => 'image',
  'multiple' => true,
  'expanded' => true

Now, in my edit.html.twig template, if I write this

{% for img in edit_form.immagini %}
    {{ form_widget(img, {'attr': {'style': 'vertical-align: top'}} ) }}
    {{ form_label(img, null,  { 'label_attr': {'style': 'display: inline'}} ) }}
{% endfor %}

...and the result of embedded images inside a gallery edit page, is correct and works great.
enter image description here

Now, what I want to do is to display in the same li tag, another Immagine field, let's say imageName. I've tryed so many ways, but I'm not able to do it...

Any suggestion?

share|improve this question
add comment

1 Answer

You should embed your associated entity by using collection type, not entity type like you did.

->add('immagini','collection',array('type'=>new ImageType(),'multiple'=>...,))

So, you need to implement a new form class, call it what you like eg. ImageType(). In that class type add whatever form types(properties) you would need to display, eg.


Than you would be able to iterate and display it from twig code.

Take a look at basic form embedding docs: http://symfony.com/doc/current/cookbook/form/form_collections.html

share|improve this answer
...no, it doesn't work. Without the multiple option in the formBuilder it shows the form of a single Immagine, and the multiple option it says me that does not exist. Can't I call the entity method to retrieve an attribute? –  ilSavo Aug 23 '13 at 14:55
add comment

Your Answer


