Yii2 Gridview Displaying 4 identical Pages of Resu

2019-09-14 23:39发布


I am working on a web application based off of the yii2 advanced application template. I have written a database query and implemented it with findbysql() returning the correct records via a grid-view generated from the gii CRUD. However for some reason 4 pages of identical results are returned. Each page is showing identical primary keys (1-10) in order, while the element index increases linearly across all 4 pages (1-40).

I have commented out the action column so i figured i did not need to include the CRUD .php files

videoController.php

public function actionIndex()
{

    $sql =  'SELECT videos.idvideo, videos.filelocation, events.event_type, events.event_timestamp
                    FROM (((ispy.videos videos
                        INNER JOIN ispy.cameras cameras
                            ON (videos.cameras_idcameras = cameras.idcameras))
                        INNER JOIN ispy.host_machines host_machines
                            ON (cameras.host_machines_idhost_machines =
                                    host_machines.idhost_machines))
                        INNER JOIN ispy.events events
                            ON (events.host_machines_idhost_machines =
                                    host_machines.idhost_machines))
                        INNER JOIN ispy.staff staff
                            ON (events.staff_idreceptionist = staff.idreceptionist)
                    WHERE     (staff.idreceptionist = 182)
                            AND (events.event_type IN (23, 24))
                            AND (events.event_timestamp BETWEEN videos.start_time
                                   AND videos.end_time)'; */
        $query = Videos::findBySql($sql);

    $dataProvider = new ActiveDataProvider([
        'query' =>  $query,
    ]);

    return $this->render('index', [
        'dataProvider' => $dataProvider,
    ]);

}

View

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'idvideo',
        'event_type',
        'event_timestamp',
        'filelocation',
        //['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

Please let me know if i need to be more specific or include any additional information.


Thanks ahead

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-14 23:50

also added this in the first part of your linked question here, with explanation

SELECT videos.idvideo, videos.filelocation, events.event_type, events.event_timestamp
FROM ((videos videos
    INNER JOIN cameras cameras
        ON (videos.cameras_idcameras = cameras.idcameras))
    INNER JOIN events events
        ON (events.host_machines_idhost_machines =
            cameras.host_machines_idhost_machines))
WHERE (events.staff_idreceptionist = 182)
        AND (events.event_type IN (23, 24))
        AND (events.event_timestamp BETWEEN videos.start_time
               AND videos.end_time);
查看更多
beautiful°
3楼-- · 2019-09-14 23:57

The problem is that the pagination needs SQL modifier offset which has no effect on the result of findBySql. See the Yii doc.

You have to use $_GET['page'] and $_GET['per-page'] in your controller, e.g.:

$page = array_key_exists('page', $_GET) ? $_GET['page']-1 : 0;
$perPage = array_key_exists('per-page', $_GET) ? $_GET['per-page'] : 30;
$offset = $page * $perPage;

$sql = "SELECT ...
        OFFSET $offset LIMIT $perPage";

$dataProvider = new ActiveDataProvider([
    'query' =>  $query,
     'pagination' => [
         pageSize' => 30,
     ],
]);
查看更多
登录 后发表回答