查询中带有leftJoin的Yii2 ActiveDataProvider不会返回分页页面大小的项



我试图在某个时间向用户返回薪水和注销信息,并在每页返回10个项目,但无法实现

我尝试了以下代码

$filters = $arr["filters"];
$timev = [strtotime($filters["duration_from"]), strtotime($filters["duration_to"])]

$query = Users::find()
->leftJoin('tbl_truck_history', 'tbl_paychecks.user_id = users.id')
->leftJoin('tbl_security_login', 'tbl_security_login.user_id = users.id')
->where(["users.department"=>3])
->andWhere(['between', 'tbl_security_login.time_out', min($timev), max($timev)]);

$data = new ActiveDataProvider([
'query' => $query,
'pagination' =>[
'pageSize' => 10, //here per_page is
'page' => $arr['page']
],
]);

return ["data" => $data->getModels(),"totalRecords" => (int)$query->count()]

当我检查$data时->getModels((,它只返回数组中的3个项。我错过了什么

问题:您的ID不是唯一的(将用作密钥(。主键将在结果中多次出现,并且将被视为同一行。

克服这个问题:您需要";分组依据";您的记录。也许可以使用users.id。

$query = Users::find()
...
->groupBy(['users.id']);

如果分组方式不适合您,您需要指定行的密钥

class ActiveDataProvider extends BaseDataProvider 
...
/**
* @var string|callable|null the column that is used as the key of the data models.
* This can be either a column name, or a callable that returns the key value of a given data model.
*
* If this is not set, the following rules will be used to determine the keys of the data models:
*
* - If [[query]] is an [[yiidbActiveQuery]] instance, the primary keys of [[yiidbActiveQuery::modelClass]] will be used.
* - Otherwise, the keys of the [[models]] array will be used.
*
* @see getKeys()
*/
public $key;

在您的情况下:

$data = new ActiveDataProvider([
'query' => $query,
'pagination' =>[
'pageSize' => 10, //here per_page is
'page' => $arr['page']
],
'key' => static function($model) {return [new unique key] }
]);

最新更新