cakephp3-limit()和包含的模型
我有两个模型,名为Post和Comment,它们由Post链接,有许多评论,而Comment属于Post。 我想获取所有帖子的前五条评论。我将使用以下代码:cakephp3-limit()和包含的模型,cakephp,find,cakephp-3.0,limits,contain,Cakephp,Find,Cakephp 3.0,Limits,Contain,我有两个模型,名为Post和Comment,它们由Post链接,有许多评论,而Comment属于Post。 我想获取所有帖子的前五条评论。我将使用以下代码: $this->Posts->find('all') ->contain([ 'Comments' => function($q) { return $q ->order('created ASC')
$this->Posts->find('all')
->contain([
'Comments' => function($q) {
return $q
->order('created ASC')
->limit(5);
}
]);
这与limit()不正确。请帮忙解决这个问题。
我用了这个例子:
我试着喜欢这个(在后期模型中):
不幸的是,这不起作用。我不知道我错在哪里。你应该试试这个
在您的模型中
$this->hasMany('Comments', [
'foreignKey' => 'post_id'
]);
在控制器中
$this->Posts->find()
->contain([
'Comments' => function($q) {
return $q
->order(['created' =>'ASC'])
->limit(5);
}
]);
很遗憾,它不起作用。CakePHP是一个功能强大的框架,但有时基本的东西不能正常工作
$this->hasMany('Comments', [
'foreignKey' => 'post_id'
]);
$this->Posts->find()
->contain([
'Comments' => function($q) {
return $q
->order(['created' =>'ASC'])
->limit(5);
}
]);