cakephp如何进行嵌套连接
我设置了以下cakephp绑定关系。他们正在工作,但是如何在结果中嵌套注释的用户记录呢cakephp如何进行嵌套连接,php,cakephp,binding,nested,has-one,Php,Cakephp,Binding,Nested,Has One,我设置了以下cakephp绑定关系。他们正在工作,但是如何在结果中嵌套注释的用户记录呢 $this->Posts->bindModel(array( 'hasOne' => array( 'User' => array( 'foreignKey' => false, 'type' => 'INNER', 'cond
$this->Posts->bindModel(array(
'hasOne' => array(
'User' => array(
'foreignKey' => false,
'type' => 'INNER',
'conditions' => array('Posts.user_id = Users.id')
)
),
'hasMany' => array(
'Comment' => array(
'foreignKey' => 'post_id',
'conditions' => array('Comment.active' => 1)
)
)
));
这非常适合获得如下结果:
[1] => Array
(
[Posts] => Array
(
[title] => test post
[body] => test body
[published] =>
[id] => 15
)
[User] => Array
(
[id] => 7
[username] => admin
[password] => d0557b9de8bb6f7fb3248a017c7b67a6
[email] => frankhinchey@gmail.com
[group_id] => 1
[created] => 2011-11-21 15:19:09
)
[Comment] => Array
(
[0] => Array
(
[id] => 10
[user_id] => 7
[post_id] => 15
[text] => testdfasdfdsfasdfasdfasdfasd
[active] => 1
[created] => 2011-12-02 20:50:57
[published] => 2011-12-05 13:58:25
)
[1] => Array
(
[id] => 11
[user_id] => 7
[post_id] => 15
[text] => this is a test comment
[active] => 1
[created] => 2011-12-02 21:31:56
[published] => 2011-12-03 11:34:32
)
)
)
我的问题是如何让相关用户也发表评论?有没有一种方法可以在我的查询中嵌套注释和用户之间的hasone关系?您可以在bindModel中使用contain吗?以前从没试过。。。但值得一试
$this->Posts->bindModel(array(
'hasOne' => array(
'User' => array(
'foreignKey' => false,
'type' => 'INNER',
'conditions' => array('Posts.user_id = Users.id')
)
),
'hasMany' => array(
'Comment' => array(
'foreignKey' => 'post_id',
'conditions' => array('Comment.active' => 1),
'contain' => array('User'),
)
)
));
事实上,现在我读了你的问题,我真的不确定你想要什么。您想知道此人评论的用户id吗?你能再详细说明一下你想要什么吗