Yii constraint on linked model at query time

I ran into a restriction problem. The code I use is as follows:

$model = PostCategory::model();
  $record = $model->with(array(
    'posts'=>array(
      'order'=>'posts.createTime DESC',
      'limit'=>3,
))->findByPK($id);

I want to limit the messages requested for paging. I also tried to add

'together'=>true

after the limit, this does not help either.

Any help is appreciated.

+5
source share
3 answers

This definitely works, just verified:

$model = PostCategory::model();
$record = $model->with(array(
  'posts'=>array(
     'order'=>'posts.createTime DESC',
  ))->findByPK($id,
             array('limit'=>3,'together'=>true) // adding this works
);
+6
source

Here are the wiki on parameterized named fields .

But if you want to filter records in RELATED tables when using Relational Query, then you should use defaultScope ().

Here's the wiki on defaultScope , and also shows how to bypass defaultScope when it is not needed.

+1

Post

PostModel.php

public function recent( $limit = 3 ) {

    $this->getDbCriteria()->mergeWith(array(
        'order' => $this->getTableAlias(false, false).'.createTime DESC',
        'limit' => (int) $limit,
    ));

    return $this;
}

MyController.php

$record = $model->with('posts:recent')->findByPK($id);

And you have clean and readable code.

More information on areas http://www.yiiframework.com/doc/guide/1.1/en/database.ar#named-scopes

And this forum is how to give the parameters of your application using → from http://www.yiiframework.com/forum/index.php/topic/23358-parameterized-vs-named-scopes-question-using-yii-118/

0
source

All Articles