Pagination

Hello guys,

sorry if this is’t the right forum section, but can’t figure out where to write (and if I can).

Till now I always developed generic sites, and for this purpose it was for me enough Wordpess and Foundation. But the time is ready for a more vertical app, with a dedicated mysql db. So I’m here, trying to build a site with yii2. First question: is it better to start with yii 1.1 ?

Second question.

I started with classic “hello world” and “Country” example. I generated the code with gii. Works fine. But there are some difference with the “Country” example explained in “The Definitive Guide to Yii 2.0”. With gii, the code was generated with no pagination, for example. If think it must be way to include it, but i can’t figure out.

This is the gii generate CountryController.php:


<?php


namespace app\controllers;


use Yii;

use app\models\Country;

use app\models\CountrySearch;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;

use yii\data\Pagination; //I added this in my attempts

/**

 * CountryController implements the CRUD actions for Country model.

 */

class CountryController extends Controller

{

    public function behaviors()

    {

        return [

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'delete' => ['post'],

                ],

            ],

        ];

    }


    /**

     * Lists all Country models.

     * @return mixed

     */

    public function actionIndex()

    {

    

        $searchModel = new CountrySearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }


    /**

     * Displays a single Country model.

     * @param string $id

     * @return mixed

     */

    public function actionView($id)

    {

        return $this->render('view', [

            'model' => $this->findModel($id),

        ]);

    }


    /**

     * Creates a new Country model.

     * If creation is successful, the browser will be redirected to the 'view' page.

     * @return mixed

     */

    public function actionCreate()

    {

        $model = new Country();


        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->code]);

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }


    /**

     * Updates an existing Country model.

     * If update is successful, the browser will be redirected to the 'view' page.

     * @param string $id

     * @return mixed

     */

    public function actionUpdate($id)

    {

        $model = $this->findModel($id);


        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->code]);

        } else {

            return $this->render('update', [

                'model' => $model,

            ]);

        }

    }


    /**

     * Deletes an existing Country model.

     * If deletion is successful, the browser will be redirected to the 'index' page.

     * @param string $id

     * @return mixed

     */

    public function actionDelete($id)

    {

        $this->findModel($id)->delete();


        return $this->redirect(['index']);

    }


    /**

     * Finds the Country model based on its primary key value.

     * If the model is not found, a 404 HTTP exception will be thrown.

     * @param string $id

     * @return Country the loaded model

     * @throws NotFoundHttpException if the model cannot be found

     */

    protected function findModel($id)

    {

        if (($model = Country::findOne($id)) !== null) {

            return $model;

        } else {

            throw new NotFoundHttpException('The requested page does not exist.');

        }

    }

}

And this is the CountryController.php used in "The definitive guide…"


<?php

namespace app\controllers;

use yii\web\Controller;

use yii\data\Pagination;

use app\models\Country;

class CountryController extends Controller

{

public function actionIndex()

{

$query = Country::find();

$pagination = new Pagination([

’defaultPageSize’ => 5,

’totalCount’ => $query->count(),

]);

$countries = $query->orderBy(’name’)

->offset($pagination->offset)

->limit($pagination->limit)

->all();

return $this->render(’index’, [

’countries’ => $countries,

’pagination’ => $pagination,

]);

}

}

I try to use this part of snippet (and many others variants) in the CountryController generated with gii, but with no luck.


$pagination = new Pagination([

’defaultPageSize’ => 5,

’totalCount’ => $query->count(),

]);

[color="#006400"]/* Moved from "Miscellaneous" to "General Discussions for Yii 2.0" */[/color]

Are you sure? Add some countries (I think the defaultPageSize is 20), so with the 21st country in your database, the pagination should show itself.

But as I am also very new to yii, I can´t tell you how to configure the pagination in this example…

Cheers

[quote name=‘denkla’]…Add some countries (I think the defaultPageSize is 20), so with the 21st country in your database, the pagination should show itself.
[/quote]

That helped indeed, thanks a lot! :)

But now I can’t understand how it works. :mellow: OMG, where is this pagination enabled? :rolleyes:

Me neither but you can adjust the pagination using this line :




$dataProvider->pagination = [

    'pageSize' => 5,

];