Symfony2 - Get an entity instead of PersistentCollection in twig

Get First item of the doctrine collection in twig

if you have only 1 object on the collection then you can get it by using the first method

{% set comment = post.comments.first %}

PersistentCollection: first() method

Convert DoctrineCollection to array in twig

To convert the doctrine collection to an array you can use the getValues() method :

{% set arrayComment = post.comments.getValues %}

PersistentCollection: getValues() method


It's because you are trying to access a collection of entities directly. You have to loop your comments collection :

{% for comment in post.comments %}
    // You can get your comment entity here 
    // for example
    <p>{{comment.description}}</p>
{% endfor %}