ArrayDataProvider may be used in the following way:
Yii2的dataProvider使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$query = new Query;
$provider = new ArrayDataProvider([
'allModels' => $query->from('post')->all(),
'sort' => [
'attributes' => ['id', 'username', 'email'],
],
'pagination' => [
'pageSize' => 10,
],
]);
// get the posts in the current page
$posts = $provider->getModels();
//when we change the source posts structure,then set model with them
$modifyPosts = ModifyStruct($posts);
$posts = $provider->setModels($modifyPosts);