Use limit range in yii2?

前端 未结 2 1913
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 16:20

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

Here is my code:

  $Query = new Query;
   $Query->select([\'um.id as USERid\', \'         


        
相关标签:
2条回答
  • 2021-01-04 16:41

    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:

    • http://www.bsourcecode.com/yiiframework2/select-query-model/#offset
    • http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#offset%28%29-detail
    0 讨论(0)
  • 2021-01-04 16:53

    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();
    
    0 讨论(0)
提交回复
热议问题