Yii 2.0实现联表查询加搜索分页的方法示例

前言

最近在学习yii2.0,在使用yii2.0过程中遇到一些问题,现将查询搜索分页的方法整理如下,分享出来供大家参考学习,话不多说,来一起看看详细的介绍:

主表:{{%article}}

关联表:{{%article_class}}

方法如下

1、使用gii创建CRUD和search不详述

2、在Article中添加的关联内容,代码#注释部分

class Article extends \yii\db\ActiveRecord 
{ 
 #关联查询1:这里加上被关联字段 
 public $class_name; 
... 
 public function rules() 
 { 
  return [ 
   [['article_title','article_content'], 'required'], 
   [['article_content','article_title','article_class'], 'string'], 
   [['article_addtime', 'article_updatetime'], 'integer'], 
   [['article_title', 'article_author'], 'string', 'max' => 50], 
   #关联查询2:这里加上safe验证,表示该表单字段无验证规则 
   ['class_name','safe'], 
  ]; 
 } 
... 
 #关联查询3:获取被关联表 mysite_article_class 
 public function getArticleClass(){ 
  /** 
  * 第一个参数为要关联的子表模型类名称, 
  * 第二个参数指定通过子表的 id 去关联主表的 article_class 字段 
  */ 
  return $this->hasMany(ArticleClass::className(), ['id' => 'article_class']); 
 } 
... 
}

3、在ArticleSearch中添加的查询和关联内容,代码#注释部分

class ArticleSearch extends Article 
{ 
 #关联查询1:这里加上被关联字段 
 public $class_name; 
... 
 public function rules() 
 { 
  return [ 
   [['id', 'article_addtime', 'article_updatetime'], 'integer'], 
   [['article_title', 'article_content', 'article_class', 'article_author'], 'safe'], 
   #关联查询2:这里加上safe验证,表示该表单字段无验证规则 
   ['class_name','safe'], 
  ]; 
 } 
... 
 public function search($params) 
 { 
  $query = Article::find(); 
 
  // add conditions that should always apply here 
  #关联查询4:使用jionWith和select做关联查询 
  $query = Article::find(); 
  $query->joinWith(['articleClass']); 
  $query->select("{{%article}}.*,{{%article_class}}.class_name"); 
   
  $dataProvider = new ActiveDataProvider([ 
   'query' => $query, 
  ]); 
   
  $this->load($params); 
  if (!$this->validate()) { 
   return $dataProvider; 
  } 
   
  // grid filtering conditions 
  #精确查询 
  $query->andFilterWhere([ 
   'id' => $this->id, 
   'article_addtime' => $this->article_addtime, 
   'article_updatetime' => $this->article_updatetime, 
   #关联查询5:添加被关联字段的精确查询,这里要跟view表单被查询属性一致, 
//   '{{%article_class}}.class_name' => $this->class_name, 
  ]); 
   
  #模糊查询 
  $query->andFilterWhere(['like', 'article_title', $this->article_title]) 
   ->andFilterWhere(['like', 'article_content', $this->article_content]) 
   ->andFilterWhere(['like', 'article_class', $this->article_class]) 
   ->andFilterWhere(['like', 'article_author', $this->article_author]) 
   #关联查询5:添加被关联字段的精确查询,这里要跟view表单被查询属性一致, 
   ->andFilterWhere(['like', '{{%article_class}}.class_name', $this->class_name]); 
  return $dataProvider; 
 } 
... 
}

4、在ArticleController中添加的分页内容,代码#注释部分

public function actionIndex() 
 {  
  $article = new Article(); 
  #查询 
  $searchModel = new ArticleSearch(); 
  $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 
  #分页 
  $dataProvider->pagination = ['pagesize' => '3']; 
  return $this->render('index', [ 
   'dataProvider' => $dataProvider, 
   'model' => $article, 
   'searchModel' => $searchModel, 
  ]); 
 }

5、在index view中添加的表单内容,代码#注释部分

<?= GridView::widget([ 
 'dataProvider' => $dataProvider, 
 #查询表单 
 'filterModel' => $searchModel, 
 'columns' => [ 
  [ 
   'class' => 'yii\grid\SerialColumn', 
   'header' => '编号', 
  ], 
//  'article_class', 
  #注意这里被关联表字段是{{%article_class}}.class_name,表单属性这么写'attribute' => 'class_name', 
  #查询结果就是被关联表字段值'value' => 'class_name', 
  [ 
   'label'=>'文章分类', 
   'attribute' => 'class_name', 
   'value' => 'class_name', 
 
  ], 
  'article_title', 
  'article_addtime:datetime', 
  // 'article_updatetime:datetime', 
  // 'article_author', 
  [ 
   'class' => 'yii\grid\ActionColumn', 
   'header' => '操作', 
  ], 
 ], 
]); ?>

以上步骤完成结果如图:

Yii 2.0实现联表查询加搜索分页的方法示例

总结

yii

相关推荐