Как создать пейджинг в Yii2?
Я хочу создать строку кода для подкачки страниц в моем веб- сервисе с Yii2, и я хочу получить результаты, как в Api The Movie есть параметр page = 1. После того, как я сделал наблюдение в Google, я нашел способ сделать подкачку, добавив ограничение и смещения в моем контроллере, но результат не получается, смещение добавляет только один идентификатор, например, результат из json 1 - 10, если я добавляю смещение в контроллере, он показывает результат 2 - 11
Мой контроллер
public function actionGetdatapage($EmployeeId, $LeaveGroup, $Status, $Flag){
$param['EmployeeId'] = $EmployeeId;
$param['LeaveGroup'] = $LeaveGroup;
$param['Status'] = $Status;
$param['Flag'] = $Flag;
return $this->getListrequestpage($param);
}
public function getListrequestpage($param){
\Yii::$app->response->format = \yii\web\Response:: FORMAT_JSON;
$data = HrAttLeaveReq::find()
->joinwith('employee')
->joinwith('dept')
->joinwith('branch')
->joinwith('leavetype')
->joinwith('position')
->where([
'EmployeeId' => $param['EmployeeId'],
'LeaveGroup' => $param['LeaveGroup'],
'Status' => $param['Status'],
'HrAttLeaveReq.Flag' => $param['Flag']
])
->limit(10)
->offset(0)
->asArray()
->orderBy(['ReqNo' => SORT_ASC])
->all();
$array = array();
$i = -1;
foreach($data as $d){
$i++;
$array[$i]['Method'] = 'Request';
$array[$i]['ReqNo'] = $d['ReqNo'];
$array[$i]['Branch'] = $d['branch']['Name'];
$array[$i]['DeptId'] = $d['employee']['DeptId'];
$array[$i]['Departement_Position'] = $d['dept']['Departement'] . ', ' . $d['position']['Position'];
$array[$i]['Name'] = $d['employee']['Name'];
$array[$i]['Photo'] = $d['employee']['Photo'];
$array[$i]['Gender'] = $d['employee']['Gender'];
if($d['StartDate'] == $d['EndDate']){
$array[$i]['Date'] = date('l, d M Y', strtotime($d['StartDate']));
}else{
$array[$i]['Date'] = date('l, d M Y', strtotime($d['StartDate'])) . ' - ' . date('l, d M Y', strtotime($d['EndDate']));;
}
$array[$i]['LeaveGroup'] = $d['LeaveGroup'];
$array[$i]['LeaveType'] = $d['LeaveType'];
$array[$i]['Code_Status'] = $d['Status'];
$array[$i]['Type'] = $d['leavetype']['Type'];
$array[$i]['Description'] = '- ' . $d['Description'];
if ($d['Status'] == 0) {
$array[$i]['Status'] = "PENDING";
}
if ($d['Status'] == 1) {
$array[$i]['Status'] = "APPROVED";
}
if ($d['Status'] == 2) {
$array[$i]['Status'] = "DENIED";
}
$array[$i]['Flag'] = $d['Flag'];
}
$result = array();
$result['value'] = empty($array) ? 0 : 1;
$result['status'] = true;
$result['result'] = $array;
return $result;
}
Результат
{
"value": 1,
"status": true,
"result": [
{
"Method": "Approval",
"ReqNo": "RO170363",
"Branch": "DCA CINERE",
"DeptId": "IT",
"Departement_Position": "Finance & Accounting, Developer",
"Name": "ALDAN RIZKI",
"Photo": "male.png",
"Gender": "m",
"Date": "Friday, 08 Sep 2017",
"LeaveGroup": "S",
"LeaveType": "S",
"Code_Status": "1",
"Type": "SAKIT",
"Description": "- This is Description ! ",
"Status": "APPROVED",
"Flag": "1"
}
]
}
Мне нужен результат как в Api The Movie
{
"page": 1,
"total_results": 7052,
"total_pages": 353,
"results": [
{
"vote_count": 580,
"id": 19404,
"video": false,
"vote_average": 9,
"title": "Dilwale Dulhania Le Jayenge",
"popularity": 43.694292,
"poster_path": "/2gvbZMtV1Zsl7FedJa5ysbpBx2G.jpg",
"original_language": "hi",
"original_title": "Dilwale Dulhania Le Jayenge",
"genre_ids": [
35,
18,
10749
],
"backdrop_path": "/nl79FQ8xWZkhL3rDr1v2RFFR6J0.jpg",
"adult": false,
"overview": "Raj is a rich, carefree, happy-go-lucky second generation NRI. Simran is the daughter of Chaudhary Baldev Singh, who in spite of being an NRI is very strict about adherence to Indian values. Simran has left for India to be married to her childhood fiancé. Raj leaves for India with a mission at his hands, to claim his lady love under the noses of her whole family. Thus begins a saga.",
"release_date": "1995-10-20"
},
]
}
Итак, как добавить параметр для страницы = 1?
1 ответ
Попробуйте использовать что-то вроде этого в методе вашей модели:
$query = self::find();
$countQuery = clone $query;
$pages = new \yii\data\Pagination(['totalCount' => $countQuery->count()]);
$pages->defaultPageSize = $pageSize;
$model = $query->offSet( $pages->offset )
->limit( $pages->limit )
->all();
return ['model' => $model, 'pages' => $pages];