简体   繁体   中英

Use limit range in yii2?

I want to get data from db using limit 12,20 .

Here is my code:

  $Query = new Query;
   $Query->select(['um.id as USERid', 'um.first_name', 'um.last_name',   'um.email', 'COUNT(g.id) as guestCount'])
 ->from('user_master um')
 ->join('LEFT JOIN', 'guest g', 'g.user_id = um.id')
 ->limit(12,20)
 ->groupBy('um.id')
 ->orderBy(['um.id' => SORT_DESC]);

  $command = $Query->createCommand();
  $evevtsUserDetail = $command->queryAll(); 

It is not working. It is giving me all rows. I also tried ->limit([12,20]) , not working.

But when I am using limit(12) then I am getting 12 rows.

I want to get rows in limit 12,20 . What should I have to do for that in my this code?

Try this:

$Query = new Query;
$Query->select(['um.id as USERid', 'um.first_name', 'um.last_name','um.email','COUNT(g.id) as guestCount'])
->from('user_master um')
->join('LEFT JOIN', 'guest g', 'g.user_id = um.id')
->limit(20)
->offset(12)
->groupBy('um.id')
->orderBy(['um.id' => SORT_DESC]);

Offset() specifies the starting point and limit() specifies the Number of records. If you want records between 12 and 20 then use limit(8) .

For More Info:

you can do with Active record

$model = YourModel::find()->where('user_id = :user_id', [':user_id' => \Yii::$app->user->id])->limit(12,20)->all();

OR

$model = YourModel::find()->where('user_id = :user_id', [':user_id' => \Yii::$app->user->id])->with(['job','job.jobRecipient'])->limit(12)->all();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM