2014-09-23 76 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