我想创建一行代码来在我的 Web服务中使用 Yii2 创建分页,我希望结果如Api The Movie中有param < strong>页= 1 即可。在Google上进行观察后,我找到了一种方法,通过在我的控制器中添加限制和偏移来进行分页,但结果失败,偏移只添加一个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"
}
]
}
中的结果
{
"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 添加参数?
答案 0 :(得分:0)
尝试在模型方法中使用类似的东西:
$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];