Php 查询时对相关模型的Yii限制

Php 查询时对相关模型的Yii限制,php,sql,yii,limit,Php,Sql,Yii,Limit,我遇到了极限问题。我使用的代码如下所示: $model = PostCategory::model(); $record = $model->with(array( 'posts'=>array( 'order'=>'posts.createTime DESC', 'limit'=>3, ))->findByPK($id); 我想限制为分页目的查询的帖子。我也试着补充一下 'together'=>true 在限制之后,这

我遇到了极限问题。我使用的代码如下所示:

$model = PostCategory::model();
  $record = $model->with(array(
    'posts'=>array(
      'order'=>'posts.createTime DESC',
      'limit'=>3,
))->findByPK($id);
我想限制为分页目的查询的帖子。我也试着补充一下

'together'=>true
在限制之后,这也没有帮助


感谢您的帮助

您可以在Post模型中添加范围并使用

PostModel.php MyController.php 你有清晰易读的代码

查看有关作用域的更多信息

这个论坛发布了如何使用->和

这肯定会起作用,刚刚测试过:

$model = PostCategory::model();
$record = $model->with(array(
  'posts'=>array(
     'order'=>'posts.createTime DESC',
  ))->findByPK($id,
             array('limit'=>3,'together'=>true) // adding this works
);
这里有一个维基

但是,如果您想在使用关系查询时过滤相关表中的记录,那么应该使用defaultScope()


这里有一个wiki,它还显示了在不需要时如何绕过defaultScope。

+1好问题,我检查了执行的查询,
limit
从未添加到查询中。如果需要任何澄清,请告诉我,以及它是如何为您解决的。
$record = $model->with('posts:recent')->findByPK($id);
$model = PostCategory::model();
$record = $model->with(array(
  'posts'=>array(
     'order'=>'posts.createTime DESC',
  ))->findByPK($id,
             array('limit'=>3,'together'=>true) // adding this works
);