Pagination and sorting data
In the latest Yii releases, the focus was moved from using Active Record directly, to grids, lists, and data providers. Still, sometimes it is better to use Active Record directly. Let's see how to list paginated AR records with the ability to sort them. In this section, we would like to create a list of films and sort them by some attributes from a database. In our example, we will sort our films by film title and rental rate attributes.
Getting ready
Create a new application using the Composer package manager, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.
Download the Sakila database from http://dev.mysql.com/doc/index-other.html.
Execute the downloaded SQLs; first schema, then data.
Configure the DB connection in
config/main.php
to use the Sakila database.Use Gii to create the
Film
model.
How to do it…
First, you need to create
@app/controllers/FilmController.php
:<?php namespace app\controllers; use app...