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

标签 php mysql yii limit

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

相关文章:

php - (UPD) 在 yii 上不限制表格输入

php - Yii - 使用高级搜索对自定义 CGridView 进行查询操作

php - Yii2:Kartik Gridview 导出配置?

php - 如何从android向mysql服务器发送数据?

php - 尝试使用 ajax 将参数传递给模式?

php - 循环显示其他表中的值

mysql - vb.net 代码可以在本地运行,但不能在 Web 服务器上运行

php - MySQL 选择 SUM JOIN WHERE 在一起不起作用

php - 在 Yii CDbCriteria 语句中使用 CASE

php - 结帐前更改购物车项目订阅属性值