yii2 数据提供者 dataProvider

数据提供者 dataProvider

yii2 数据提供者 dataProvider

        $dataProvider = new ActiveDataProvider([
'query' => $query, // 如何来取得数据
'pagination' => ['pageSize'=>5], // pagination 用于分页
'sort' => [ // sort 用于排序
'defaultOrder' => [
'id' => SORT_DESC, // defaultOrder 指定默认排序字段
],
'attributes' => ['id','title'], // attribute 指定那几个字段可以用来排序
],
]);

yii2 数据提供者 dataProvider

如果需要打印原生sql,使用如下方法:

  echo $query ->createCommand()->getRawSql();
$dataProvider->query->each() //获取 $dataProvider 所有记录;
$dataProvider->getModels() // 获取当前页的数据,以数组的方式返回

由于 $dataProvider->getModels() 获取的数据是当前页的,如果在导出的时候需要导出所有记录而不是当前页,且又使用的是 SqlDataProvider ,那么就可以在控制器文件中在重新设置一下分页,将值设置的特别大,就可以模拟为不分页了。

use yii\db\Pagination;
$dataProvider = $searchModel->search(\Yii::$app->request->getQueryParams());
$dataProvider->setPagination(new Pagination([
'defaultPageSize' => 10000,
'pageSizeLimit' => [1, 10000]
]));

二、Yii的数据提供者类都包含:

如果需要使用 UNION 联合多张表查询,且查询中还可能包含 关联查询(join)的时候,使用 SqlDataProvider 来实现如下:

<?php
namespace organize\models; use Yii;
use yii\base\Model;
use standard\models\Country;
use organize\models\User;
use yii\db\Expression;
use yii\data\SqlDataProvider; class CountrySearch extends Country
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['country_id', 'created_at', 'updated_at', 'is_delete'], 'integer'],
[[ 'country_name','remarks'], 'safe'],
];
} /**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
} /**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return SqlDataProvider
*/
public function search($params)
{
$query = self::find()
->select(['country_id AS unique_id','country_name','created_at'])
->addSelect(new Expression("'country' AS table_name")); // 查询一个常量 $this->load($params); // grid filtering conditions
$query->andFilterWhere([
'country_id' => $this->country_id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'is_delete' => $this->is_delete,
]); $query->andFilterWhere(['like', 'country_name', $this->country_name])
->andFilterWhere(['like', 'remarks', $this->remarks]);
$query->from('standard.country'); // 有多个库名的时候使用 $userQuery = User::find();
$userQuery->innerJoin('organize.org_auth_assignment','org_user.id=org_auth_assignment.user_id')
->select(['id AS unique_id','realname','sex','dept_id','mobile','email','qq','org_auth_assignment.name as role','org_user.created_at'])
->addSelect(new Expression("'org_user' AS table_name")); $userQuery->andFilterWhere(['like', 'realname', $this->realname])
->andFilterWhere(['like', 'sex', $this->sex])
->andFilterWhere(['=', 'org_auth_assignment.name', $this->role])
->andFilterWhere(['like', 'mobile', $this->mobile])
->andFilterWhere(['like', 'email', $this->email])
->andFilterWhere(['like', 'qq', $this->qq]);
$userQuery->from('organize.org_user'); $query->union($userQuery,true); // 联合查询 $sql = $query->createCommand()->rawSql; // 生成一条不带分页的 sql
$dataProvider = new SqlDataProvider([
'sql' => $sql,
'totalCount' => $query->count(),
'sort' => false,
'pagination' => [
'pageSize' => 20,
],
]); if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
} //echo $query->createCommand()->getRawSql();
return $dataProvider;
}
}
// 最后的 $dataProvider 渲染到页面后,用 ArrayHelper() 即可获取对应的值

关于 $dataProvider 更详细的内容,请参考官网:http://www.yiichina.com/doc/guide/2.0/output-data-providers

注:本文为作者(44106-kangaroo) 看完魏羲教你学Yii2.0 视频后所记,如有转载请注明出处:http://www.cnblogs.com/chrdai/p/7966640.html

上一篇:IIS 修改并发连接数


下一篇:如何在 Centos7 中使用阿里云的yum源