0

我想创建一行代码来使用Yii2在我的Web 服务中创建分页,并且我想要Api The Movie中的结果有 param page=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"
        }
    ]
}

我需要像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添加参数?

4

1 回答 1

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];
于 2017-09-20T21:48:54.813 回答