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 through Query Builder, as shown in the following example:
$query = new yii\db\Query(); $provider = new ActiveDataProvider([ 'query' => $query->from('user'), 'pagination' => [ 'pageSize' =>...