Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get count of Post comments to display it in backend Posts list?
    text
    copied!<p>If it helps, here is my schema for Posts and Comments:</p> <pre><code>BlogPost: actAs: Timestampable: ~ I18n: fields: [title, body] tableName: blog_posts columns: #id: { type: integer(4), primary: true, autoincrement: true } user_id: { type: integer } title: { type: string(255) } body: { type: text } relations: User: type: one class: sfGuardUser local: user_id foreign: id Comments: type: many class: BlogComment local: id foreign: post_id BlogComment: actAs: Timestampable: ~ tableName: blog_comments columns: #id: { type: integer(4), primary: true, autoincrement: true } post_id: { type: integer } user_id: { type: integer } body: { type: text } relations: Post: type: one class: BlogPost local: post_id foreign: id </code></pre> <p>Here is my Post Query class:</p> <pre><code>class BlogPostQuery extends Doctrine_Query { /** * * @param Doctrine_Connection $conn * @param string $class * @return BlogPostQuery */ public static function create($conn = null, $class = null) { return parent::create($conn, 'BlogPostQuery') -&gt;from('BlogPost p'); } /** * * @param string $fields * @return BlogPostQuery */ public function addSelf($fields = 'p.*') { return $this -&gt;addSelect($fields); } /** * * @param string $fields * @return BlogPostQuery */ public function addUsers($fields = 'u.*') { return $this -&gt;addSelect($fields) -&gt;innerJoin('p.User u') -&gt;addGroupBy('u.id'); } /** * * @param string $fields * @return BlogPostQuery */ public function addComments($fields = 'pc.*') { return $this -&gt;addSelect($fields) -&gt;leftJoin('p.Comments pc') -&gt;addGroupBy('p.id'); } public function addCommentsCount($alias = 'nb_comments') { return $this -&gt;addSelect(sprintf('COUNT(pc.id) as %s', $alias)) -&gt;addGroupBy('p.id'); } } </code></pre> <p>Here is my Post Table class:</p> <pre><code>class BlogPostTable extends Doctrine_Table { /** * Returns an instance of this class. * * @return object BlogPostTable */ public static function getInstance() { return Doctrine_Core::getTable('BlogPost'); } public static function findAllWithCountComments() { return BlogPostQuery::create() -&gt;addSelf() -&gt;addUsers() -&gt;addComments() -&gt;addCommentsCount() -&gt;execute(); } } </code></pre> <p>So in generator.yml in list config I write:</p> <pre><code>list: title: Blog Posts Managment display: [id,title,User,nb_comments] table_method: findAllWithCountComments </code></pre> <p>But id doesn't work and throws error: </p> <blockquote> <p>Unknown record property / related component "nb_comments" on "BlogPost"</p> </blockquote> <p>Also there are too many queries to DB.</p> <p>So how to overcome this error and get count of comments for each post?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload