Yii 2.0 paging function implementation

巴扎黑
Release: 2023-03-14 17:36:02
Original
1162 people have browsed it

This article mainly introduces you to the method of using Yii 2.0 to implement joint table query and search paging. The article introduces it in great detail through sample code. It has certain reference learning value for everyone's study or work. Friends who need it Let’s learn with the editor below.

Preface

I have recently been learning yii2.0 and encountered some problems while using yii2.0. Now I will query and search for pagination. The methods are organized as follows and shared for everyone’s reference and study. Without further ado, let’s take a look at the detailed introduction:

Main table: {{%article}}

Association table: {{%article_class}}

##The method is as follows

1. Use gii to create CRUD and search without going into details


2. The associated content added in Article, code# comment part


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']); 
 } 
... 
}
Copy after login

3. The query and associated content added in ArticleSearch , code # comment part


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; 
 } 
... 
}
Copy after login

4. Paging content added in ArticleController, code # comment part


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, 
  ]); 
 }
Copy after login

5. The form content added in the index view, code# comment part


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

The result of completing the above steps is as shown in the figure:

The above is the detailed content of Yii 2.0 paging function implementation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!