Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Unrecognized field: created_at while trying to get number of comments #25

Closed
reverso73 opened this issue Oct 18, 2011 · 7 comments
Closed

Comments

@reverso73
Copy link

Hello,

I try to display on my "archive" page the number of comments for each post.
When I try to access to the property like this in archive.html.twig

{% for post in pager.getResults() %}
{{ post.getComments|length }}
{% else %}
{{ 'no_post_found'|trans({}, 'SonataNewsBundle') }}
{% endfor %}

I get :

An exception has been thrown during the rendering of a template ("Unrecognized field: created_at")

Do you have a trick to get the number of comments, validated if possible, for each post ?

Thanks

@rande
Copy link
Member

rande commented Oct 30, 2011

Can you be more specific about this issue ?

@pulse00
Copy link
Contributor

pulse00 commented Nov 23, 2011

I'm getting the same error for the following template:

<div class="post">
    {{ post.title }}    
    {% media post.image, 'big' %}    
    <div  class="comments">
        {% for comment in post.comments %} 
          {% include 'MyBundle:Default:comment.html.twig' with { 'comment' : comment } %}         
        {% endfor %}    
    </div>
</div>

It happens when doctrine tries to load the OneToMany association:

(array('t0.post_id' => '1'), 
    array('fieldName' => 'comments', 
        'targetEntity' => 'Application\Sonata\NewsBundle\Entity\Comment', 
        'mappedBy' => 'post', 
        'cascade' => array('persist'), 
        'orphanRemoval' => true, 
        'orderBy' =>  array('created_at' => 'DESC'), 
        'type' => '4', 
        'inversedBy' => null, 
        'isOwningSide' => false, 
        'sourceEntity' => 'Application\Sonata\NewsBundle\Entity\Post', 
        'fetch' => '2', 
        'isCascadeRemove' => true, 
        'isCascadePersist' => true, 
        'isCascadeRefresh' => false, 
        'isCascadeMerge' => false, 
        'isCascadeDetach' => false)
    , '0', null, null
) 

@pulse00
Copy link
Contributor

pulse00 commented Nov 23, 2011

The reason is that the Post.orm.xml file in the ApplicationSonataNewsBundle was created with a wrong order_by node by the easy-extends Bundle.

Generated order-by:

 <order-by>
    <order-by-field name="created_at" direction="DESC" />
</order-by>

Shoud be:

<order-by>
    <order-by-field name="createdAt" direction="DESC" />
</order-by>

@rande
Copy link
Member

rande commented Nov 23, 2011

can you create a PR ?

@pulse00
Copy link
Contributor

pulse00 commented Nov 23, 2011

sure, but it will take some time as i'm really busy atm.

@pulse00
Copy link
Contributor

pulse00 commented Nov 28, 2011

the PR is here, could you merge it?

It also contains german translations.

@pulse00
Copy link
Contributor

pulse00 commented Nov 28, 2011

thanks for merging. i guess this is solved then.

@rande rande closed this as completed Dec 5, 2011
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants