0

役立つ場合は、投稿とコメントのスキーマを次に示します。

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

これが私のPost Queryクラスです:

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');
        }
     }

これが私のポストテーブルクラスです:

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_comments」

また、DB へのクエリが多すぎます。

では、このエラーを克服し、各投稿のコメント数を取得するにはどうすればよいでしょうか?

4

1 に答える 1

0

nb_comments の BlogPost クラスにメソッドを追加する必要があります

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

http://www.symfony-project.org/gentle-introduction/1_4/en/14-Admin-Generatorの「カスタム フィールド」セクションを参照してください。

于 2011-04-19T10:22:02.017 に答える