O'Reilly logo

Mastering Yii by Charles R. Portwood II

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Data providers and data widgets

In Yii2, data providers are helper classes that are used to extract data via Query Builder to be passed to a data widget. The benefit of using data providers and data widgets over queries built via Query Builder is that they provide an interface to automatically deal with sorting and pagination.

The most common way to work with data providers is to use the yii\data\ActiveDataProvider class. Typically, yii\data\ActiveDataProvider will be used with Active Record models:

$provider = new ActiveDataProvider([
    'query' => User::find(),
    'pagination' => [
        'pageSize' => 20,
    ],
]);

Tip

We'll cover how to create and use Active Record and models in Chapter 4, Active Record, Models, and Forms.

Active data providers can also be populated ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required