我有一个博客文章模型:
.ToList()
博客评论模型:
App::uses('AppModel', 'Model');
class BlogPost extends AppModel {
public $primaryKey = "ID";
public $useDbConfig = 'dev_blog';
public $useTable = 'posts';
public $hasMany = array(
'BlogComment' => array(
'foreignKey' => 'comment_post_ID',
'className' => 'BlogComment',
'order' => 'BlogComment.comment_date DESC',
'conditions' => array(
'BlogComment.comment_approved' => '1',
'BlogComment.comment_parent' => 0
)
)
);
}
在控制器中,我在BlogPost上设置等于2的递归并执行查找:
App::uses('AppModel', 'Model');
class BlogComment extends AppModel {
public $primaryKey = "comment_ID";
public $useDbConfig = 'dev_blog';
public $useTable = 'comments';
public $hasMany = array(
'Children' => array(
'className' => 'BlogComment',
'foreignKey' => 'comment_parent',
'order' => 'Children.comment_date DESC',
'conditions' => array(
'Children.comment_approved' => 1
)
)
);
}
这样做可以获取特定博客文章的内容,所有根评论和对根评论的一级回复。但是,它没有回复回复。如何检索任意深度的回复?
答案 0 :(得分:3)
根本不要使用recursive
。如初。
在AppModel中将其设置为-1:
public $ recursive = -1;
然后忘记你甚至听说过它,并使用CakePHP的真棒Containable Behavior来获取你需要的数据。
注意:请参阅MANY other answers related to using CakePHP's Containable。