Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii limit on related model while querying

Tags:

sql

php

limit

yii

I ran into limit problem. The code that I'm using is the following:

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

I want to limit the posts queried for paging purposes. I've also tried to add

'together'=>true

after limit, this doesn't help too.

Any help is appreciated.

like image 979
Arman P. Avatar asked Mar 28 '12 22:03

Arman P.


2 Answers

This will definitely work, just tested :

$model = PostCategory::model();
$record = $model->with(array(
  'posts'=>array(
     'order'=>'posts.createTime DESC',
  ))->findByPK($id,
             array('limit'=>3,'together'=>true) // adding this works
);
like image 105
bool.dev Avatar answered Nov 07 '22 18:11

bool.dev


Here is a wiki on parameterized named scopes.

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

Here is a wiki on defaultScope and it also shows how to bypass defaultScope when it is not needed.

like image 45
Gerhard Liebenberg Avatar answered Nov 07 '22 18:11

Gerhard Liebenberg