1

我对 cakephp 2.x 和模型的关系有一些问题,我有 3 个模型,帖子、用户和评论。我想将帖子与他的用户绑定,并将评论与他的用户绑定。

在后期模型中:

   $belongsTo = array('Comment','User'),
   $hasMany = array('CommentOfPost'=>array('className'=>'Comment'));

我有与用户绑定的帖子,以及帖子中的所有评论,但我没有评论的用户。

编辑 :

sql输出

   1    SELECT `Post`.`id`, `Post`.`title`, `Post`.`content`, `Post`.`tags`, `Post`.`created`,  `Post`.`modified`, `Post`.`user_id`, `User`.`id`, `User`.`username`, `User`.`password`, `User`.`role`, `User`.`name`, `User`.`lastname`, `User`.`birthdate`, `User`.`email`, `User`.`created`, `User`.`modified` FROM `blog`.`posts` AS `Post` LEFT JOIN `blog`.`users` AS `User` ON (`Post`.`user_id` = `User`.`id`) WHERE `Post`.`id` = 16 LIMIT 1        1   1   0
   2    SELECT `CommentOfPost`.`id`, `CommentOfPost`.`post_id`, `CommentOfPost`.`user_id`, `CommentOfPost`.`content`, `CommentOfPost`.`created` FROM `blog`.`comments` AS `CommentOfPost` WHERE `CommentOfPost`.`post_id` = (16)

编辑:评论模型

    public $belongsTo = array('User' => array('className' => 'User'));

后模型

    public $belongsTo = array('Comment' => array('className' => 'Comment'));

编辑 :

感谢您的回复,我有相同的结果,就像我有帖子和他的用户和帖子和他的评论,但不是评论的用户

现在我的模特帖子

    $hasMany = array(
            'Comment' => array(
                'className' => 'Comment',
            )
        ),
        $belongsTo = array(
            'User' => array(
                'className' => 'User',
            )
        );

用户模型

   $hasMany = array('Comment' => array('className' => 'Comment'));

评论模型

   $belongsTo = array('Users' => array('className' => 'User'), 'Posts' => array('clasName' => 'Post'));

我在 PostsController 中使用分页查询

    $this->Paginator->settings = $this->paginate;

    $data = $this->Paginator->paginate('Post');
    $this->set('posts', $data);

编辑 :

我的分页参数

    $paginate = array(
    'limit' => 10,
    'recursive' => 2,
    'order' => array(
        'Post.id' => 'desc'
    )
);

我尝试使用和不使用递归选项

好的,完成了!

对于简历,我有:

   class User extends AppModel {
       public $hasMany = array('Comment' => array('className' => 'Comment'));
   }

   class Post extends AppModel 
   {
       $hasMany = array(
            'Comment' => array(
                'className' => 'Comment',
            )
        ),
        $belongsTo = array(
            'User' => array(
                'className' => 'User',
            )
        );
    }

    class Comment extends AppModel {
        public $belongsTo = array('User' => array('className' => 'User'), 'Post' => array('clasName' => 'Post'));

}

    PostsController extends AppController
    {
        ....

        $this->Post->recursive = 2;
        $post = $this->Post->findById($id);

        ...
    }

谢谢你们的帮助,你们太棒了!

4

1 回答 1

0

Post不属于Comment。它有很多comments.

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

食谱中的示例:

class User extends AppModel {
    public $hasMany = array(
        'MyRecipe' => array(
            'className' => 'Recipe',
        )
    );
}

对于您的应用程序:

class Post extends AppModel {
    public $hasMany = array(
        'Comment' => array(
            'className' => 'Comment',
        )
    );

    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
        )
    );
}
于 2014-09-23T09:36:41.440 回答