2014-09-23 71 views
1

我對cakephp 2.x和模型的關係有一些麻煩,我有3個模型,發佈,用戶和評論。 我想用他的用戶和評論與他的用戶綁定帖子。Cakephp 2.x多重關係

在帖子型號:

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

我用PAGINATE在我的PostsController查詢

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

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

編輯:

我PAGINATE PARAMS

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

     ... 
    } 

感謝您的幫助球員,你真棒!

+0

也可以提供你的'找到()'體現在哪裏? – marian0 2014-09-23 13:20:25

+0

我編輯我的帖子。 – user3566446 2014-09-23 14:19:32

+0

你可以追加到你的分頁params數組''遞歸'=> 2'嗎? – marian0 2014-09-23 14:22:57

回答

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

另外,'User'有很多'Comment',Comment'屬於'Post'和'User'。 – 2014-09-23 09:40:17

+0

感謝您的回覆,相同的結果請參閱我的編輯。 – user3566446 2014-09-23 10:28:54

+0

也許你應該檢查你的關係想要的女巫主鍵。 http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html 在這裏你可以找到更多的選項設置到你的關係數組...如果它不會工作我們可以聊天! – Bob 2014-09-23 17:05:38