SearchModel示例
- 示例一
- 示例二
- 示例三
该模型方便用户查询,不再每次都要单独的 SearchModel
use common\models\base\SearchModel;
示例一
$searchModel = new SearchModel([
'model' => Topic::class,
'scenario' => 'default',
]);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
示例二
$searchModel = new SearchModel([
'defaultOrder' => ['id' => SORT_DESC],
'model' => Topic::class,
'scenario' => 'default',
'relations' => ['comment' => []], // 关联表(可以是Model里面的关联)
'partialMatchAttributes' => ['title'], // 模糊查询
'pageSize' => 15
]);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andWhere([Topic::tableName() . '.user_id' => 23, Comment::tableName() . '.status' => 1]);
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
示例三
关联多表查询
$searchModel = new SearchModel([
'defaultOrder' => ['id' => SORT_DESC],
'model' => Topic::class,
'scenario' => 'default',
'relations' => ['member' => ['nickname']], // 关联 member 表的 nickname 字段
'partialMatchAttributes' => ['code', 'member.nickname'], // 模糊查询,注意 member_nickname 为关联表的别名 表名_字段
'pageSize' => 15
]);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andWhere([Topic::tableName() . '.user_id' => 23, Comment::tableName() . '.status' => 1]);
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
GridView 片段
[
'attribute' => 'member.nickname',
'label'=> '昵称',
'filter' => Html::activeTextInput($searchModel, 'member.nickname', [
'class' => 'form-control'
]
),
],
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。