Jérémy Dutheil
Jérémy Dutheil

Reputation: 6137

Display Doctrine Collection with twig ? (Symfony2)

I have a quite simple doctrine entity that represent a news ; this news can be linked with many pictures, so I decided to use a Doctrine Collection. The thing is, I want to retrieve this pictures and display them in my template... But it didn't seem to work. Do you know how I can do that ?

Here is what I tried :

{% for annonce in annonces %}
    <div class="annonce_item">
    {% for photo in annonce.photo  %}
        <img src="{{ photo.path }}" alt="" />
    {% endfor %}
</div>
<!-- End .annonce_item -->
{% endfor %}

annonce is the news class, and photo is the collection :

/**
 * @ORM\OneToMany(targetEntity="Photo", mappedBy="id",cascade={"persist"})
 */

private $photo;

When I try to display this page in my browser, I get this exception:

An exception has been thrown during the rendering of a template ("Notice: Undefined index: >id in >/Applications/MAMP/htdocs/ApacheImmobilier/vendor/doctrine/lib/Doctrine/ORM/Persisters/Basi>cEntityPersister.php line 1274") in "APPagesBundle:Index:index.html.twig" at line 45.

Thanks!

Upvotes: 5

Views: 7339

Answers (1)

greg0ire
greg0ire

Reputation: 23255

Read this article of the doc. It says:

The mappedBy attribute designates the field in the entity that is the owner of the relationship.

which, in your case, must be the news field of your Photo entity.

Upvotes: 9

Related Questions