简体   繁体   English

yii \\ db \\ Query :: limit()函数不限制记录-Yii2

[英]yii\db\Query::limit() function not limiting the records - Yii2

Below is the code I'm using to get data from my table(s) for creating REST api. 以下是我用来从表中获取数据以创建REST api的代码。

$query = new yii\db\Query();
$sql = $query
    ->select('a.vehicle_number, b.device_id, b.dated, b.speed, b.ignition, b.latitude, b.longitude')
    ->from('tk103_devices a, tk103_current_location b')
    ->where('a.device_id = b.device_id AND a.transporter_id='.$id)
    ->orderBy(['a.vehicle_number'=>SORT_ASC])
    ->limit(1);

$dataProvider = new ActiveDataProvider([
    'query'=>$sql
    ]);
return array('count_flag'=>$countFlag, 'dataProvider'=>$dataProvider->getModels());

I had set the limit(1) which does " Sets the LIMIT part of the query. " as per Yii official doc http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#limit()-detail . 我已经按照Yii官方文档http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#limit()设置了limit(1)来执行“ 设置查询的LIMIT部分。-详细

When I am executing the above query, all the records are being returned by the dataprovider. 当我执行上面的查询时,所有记录都由dataprovider返回。

What's wrong with my code? 我的代码有什么问题?

ActiveDataProvider does not take care at query limit. ActiveDataProvider不在查询限制范围内。

http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html#active-data-provider http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html#active-data-provider

Extract from above link: 从上面的链接中提取:

Note: If a query already specifies the orderBy clause, the new ordering instructions given by end users (through the sort configuration) will be appended to the existing orderBy clause. 注意:如果查询已经指定了orderBy子句,则最终用户给出的新排序指令(通过sort配置)将附加到现有的orderBy子句中。 Any existing limit and offset clauses will be overwritten by the pagination request from end users (through the pagination configuration). 来自终端用户的分页请求(通过分页配置)将覆盖任何现有的limit和offset子句。

So, since you have fixed data, use ArrayDataProvider: 因此,由于您拥有固定的数据,请使用ArrayDataProvider:

$data = $query
    ->select('a.vehicle_number, b.device_id, b.dated, b.speed, b.ignition, b.latitude, b.longitude')
    ->from('tk103_devices a, tk103_current_location b')
    ->where('a.device_id = b.device_id AND a.transporter_id='.$id)
    ->orderBy(['a.vehicle_number'=>SORT_ASC])
    ->limit(1)
    ->all();

$dataProvider = new \yii\data\ArrayDataProvider(['allModels' => $data]);

Did some homework by myself, I find solution to the above problem by changing the code as below: 我自己做了一些功课,通过更改以下代码,我找到了解决上述问题的方法:

$query = new yii\db\Query();
$sql = $query
    ->select('a.vehicle_number, b.device_id, b.dated, b.speed, b.ignition, b.latitude, b.longitude')
    ->from('tk103_devices a, tk103_current_location b')
    ->where('a.device_id = b.device_id AND a.transporter_id='.$id)
    ->orderBy(['a.vehicle_number'=>SORT_ASC])
    ->one();

$dataProvider = new ActiveDataProvider([
    'query'=>$sql
    ]);
return array('count_flag'=>$countFlag, 'dataProvider'=>$dataProvider);

As per my scenario, I wanted to retrieve only first record. 根据我的情况,我只想检索第一条记录。 So, I used one() instead of limit(1) . 因此,我使用one()而不是limit(1)

Secondly, I was returning dataProvider as $dataProvider->getModels() . 其次,我将dataProvider作为$dataProvider->getModels() I changed this to $dataProvider only. 我将其更改$dataProvider$dataProvider Since "ActiveDataProvider does not take care at query limit." 由于“ ActiveDataProvider不在查询限制范围内进行处理”。 as per Fabrizio Caldarelli 's answer below (or) above, it was returning all retrieved records. 根据Fabrizio Caldarelli在下面(或上面)的回答,它正在返回所有检索到的记录。

Hope that helps someone having related issues. 希望能对遇到相关问题的人有所帮助。


For previous code to work, you must see Fabrizio Caldarelli 's answer below (or) above. 为了使先前的代码起作用,您必须在下面(或上面)看到Fabrizio Caldarelli答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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