gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:36:54 25 4
gpt4 key购买 nike

下面是我用来从表中获取数据以创建 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());

我已经设置了 limit(1),它按照 Yii 官方文档 http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#limit()-detail 执行“设置查询的 LIMIT 部分。” .

当我执行上述查询时,所有记录都由数据提供者返回。

我的代码有什么问题吗?

最佳答案

ActiveDataProvider 不关心查询限制。

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

摘自上述链接:

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. Any existing limit and offset clauses will be overwritten by the pagination request from end users (through the pagination configuration).

因此,既然您有固定数据,请使用 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]);

关于php - yii\db\Query::limit() 函数不限制记录 - Yii2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36426053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com