我想创建一行代码来使用 Yii2 在我的 Web Service 中创建分页,并且我想要像 Api The Movie 中那样的结果,其中有参数 page=1。在我对Google进行观察后,我找到了一种通过在控制器中添加limit和offsets来进行分页的方法,但结果失败,offset仅添加一个id,例如如果我添加,则来自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"
},
]
}
那么如何为 page=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];