Use limit range in yii2?

48,584

Solution 1

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:

Solution 2

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();
Share:
48,584
unknownbits
Author by

unknownbits

try to start it again

Updated on March 21, 2020

Comments

  • unknownbits
    unknownbits about 4 years

    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?

  • Kalpesh Desai
    Kalpesh Desai over 8 years
    your Model must be extends \yii\db\ActiveRecord