Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 573
Jérémy Dutheil Avatar asked Nov 27 '11 17:11

Jérémy Dutheil


1 Answers

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.

like image 172
greg0ire Avatar answered Oct 25 '22 09:10

greg0ire