DataProvider for grids

GridView is the widget provided by Yii2 to display data in a grid layout.

This widget requires that data used as an input source is an extension of the abstract class yiidataBaseDataProvider.

To deal with a data source, DataProvider supplies some additional actions to handle pagination and sorting.

BaseDataProvider has a method named getModels() that returns a list of items for the current page. This means that we could also use DataProvider to paginate data from a source and display it as we need to.

By default, the framework has three core classes that extend yiidataBaseDataProvider:

  • yiidataActiveDataProvider
  • yiidataArrayDataProvider
  • yiidataSqlDataProvider

The first one, ActiveDataProvider, uses a yiidbQuery instance from ActiveRecord as a data source. The parameter array is passed to the constructor and the yiidbQuery object is filled out in the query attribute:

// build an ActiveDataProvider with an empty query and a pagination with 35 items for page
$provider = new yiidataActiveDataProvider([
    'query' => Room::find(),
    'pagination' => [
        'pageSize' => 35,
    ],
]);

// get all rooms in current page
$rooms = $provider->getModels();

ActiveDataProvider is the most used DataProvider, since it depends directly on ActiveRecord, the best way to interact with databases.

The second point, ArrayDataProvider, uses an array of items that can be sorted or paginated as a data source. This provider is employed when data can not be represented with ActiveRecord, for example, when they are taken from another data source, such as a JSON REST service or RSS feed.

The primary difference between ActiveDataProvider is that all data should be immediately passed to a construct:

// build an ArrayDataProvider with an empty query and a pagination with 40 items for page
$provider = new yiidataArrayDataProvider([
    'allModels' => Room::find()->all(),
    'pagination' => [
        'pageSize' => 40,
    ],
]);

// get all rooms in current page
$rooms = $provider->getModels();

In this snippet, we took data from an ActiveRecord to show the differences between ActiveDataProvider and ArrayDataProvider. For this last provider, all the modes should be passed to the constructor.

So, if the Room table has 10,000 records, with ActiveDataProvider 35 items at a time will be loaded, while through ArrayDataProvider they will be loaded all from scratch (with big performance issues).

The last one, SqlDataProvider, uses a SQL query as a data source. If we create pagination with this provider, we will need to also pass the totalCount attribute to the constructor to inform DataProvider how many records the SQL query should return:

// return total items count for this sql query
$itemsCount = Yii::$app->db->createCommand('SELECT COUNT(*) FROM room')->queryScalar();

// build a SqlDataProvider with a pagination with 10 items for page

$dataProvider = new yiidataSqlDataProvider([
    'sql' => 'SELECT * FROM room',
    'totalCount' => $itemsCount,
    'pagination' => [
            'pageSize' => 10,
    ],
]);

// get the user records in the current page
$models = $dataProvider->getModels();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.105.239