【问题标题】:How to get count of Post comments to display it in backend Posts list?如何获取帖子评论的数量以将其显示在后端帖子列表中?
【发布时间】:2011-04-19 09:36:05
【问题描述】:

如果有帮助,这是我的帖子和评论模式:

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

这是我的帖子查询类:

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')
                    ->from('BlogPost p');
        }

        /**
         *
         * @param string $fields
         * @return BlogPostQuery
         */
        public function addSelf($fields = 'p.*') {
            return $this
            ->addSelect($fields);
        }

        /**
         *
         * @param string $fields
         * @return BlogPostQuery
         */
        public function addUsers($fields = 'u.*') {
            return $this
            ->addSelect($fields)
            ->innerJoin('p.User u')
            ->addGroupBy('u.id');
        }

        /**
         *
         * @param string $fields
         * @return BlogPostQuery
         */
        public function addComments($fields = 'pc.*') {
            return $this
            ->addSelect($fields)
            ->leftJoin('p.Comments pc')
            ->addGroupBy('p.id');
        }


        public function addCommentsCount($alias = 'nb_comments') {
            return $this
            ->addSelect(sprintf('COUNT(pc.id) as %s', $alias))
            ->addGroupBy('p.id');
        }
     }

这是我的 Post Table 类:

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()
                ->addSelf()
                ->addUsers()
                ->addComments()
                ->addCommentsCount()
                ->execute();
    }

}

所以在列表配置中的 generator.yml 中我写:

list:
        title: Blog Posts Managment
        display: [id,title,User,nb_comments]
        table_method: findAllWithCountComments

但是id不起作用并抛出错误:

未知记录属性/相关 “BlogPost”上的组件“nb_cmets”

还有太多对 DB 的查询。

那么如何克服这个错误并获取每个帖子的 cmets 计数?

【问题讨论】:

    标签: symfony1 doctrine admin-generator


    【解决方案1】:

    您需要将该方法添加到 nb_cmets 的 BlogPost 类中

    // BlogPost class
    public function getNbComments()
    {
        // return number of comments
    }
    

    请参阅http://www.symfony-project.org/gentle-introduction/1_4/en/14-Admin-Generator 中的“自定义字段”部分

    【讨论】:

      猜你喜欢
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 2021-11-02
      • 1970-01-01
      • 2021-03-03
      • 2022-01-10
      • 1970-01-01
      相关资源
      最近更新 更多