php 在 yii2 中使用限制范围?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32155900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use limit range in yii2?
提问by unknownbits
I want to get data from db using limit 12,20.
我想使用limit 12,20从 db 获取数据。
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.
它不工作。它给了我所有的行。我也试过 ->limit([12,20]),不工作。
But when I am using limit(12)then I am getting 12 rows.
但是当我使用 limit(12) 时,我得到了 12 行。
I want to get rows in limit 12,20. What should I have to do for that in my this code?
我想获得限制为 12,20 的行。在我的这段代码中,我应该怎么做?
回答by Insane Skull
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 12and 20then use limit(8).
Offset()指定起点并limit()指定记录数。如果你想记录之间12,20然后使用limit(8).
For More Info:
更多信息:
回答by Kalpesh Desai
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();

