簡體   English   中英

Cakephp 2.x多重關系

[英]Cakephp 2.x multiple relationship

我在cakephp 2.x和模型之間存在一些麻煩,我有3個模型,Post,User和Comment。 我想將帖子與他的用戶綁定在一起,並將評論與他的用戶綁定在一起。

在后期模型中:

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

編輯:

感謝您的答復,我得到的結果與之前收到Post和他的User以及Post和他的評論但沒有評論用戶的結果相同

現在我的模特

    $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);

        ...
    }

謝謝您的幫助,您真棒!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM