Yii2 Gridview Displaying 4 identical Pages of Results

耗尽温柔 提交于 2019-12-12 04:21:56

问题



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


回答1:


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,
     ],
]);



回答2:


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);


来源:https://stackoverflow.com/questions/41575231/yii2-gridview-displaying-4-identical-pages-of-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!