如何解决Yii2 ActiveDataProvider with leftJoin in query 不返回分页 pagesize items
我试图在某个时间返回带薪水和注销的用户,并在每个页面返回 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']);
如果 group by 不适合您,则需要指定行的键
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 [[\yii\db\ActiveQuery]] instance,the primary keys of [[\yii\db\ActiveQuery::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] }
]);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。