一、模型配置
事例会用到三个models。文章类别表和文章表用gii生成下即可,最后一个是搜索验证模型。其中,只讲下一个联表和搜索验证。其他不用操作。
1.文章表关联
?php//...other codehtml' target='_blank'>public function getCate(){ return $this- hasOne(ArticleCate::className(),[ id = cid ?
2.搜索模型
common/models/search/创建ArticleSearch.php
?phpnamespace common/models/search;use Yii;use yii/base/Model;use yii/data/ActiveDataProvider;use common/models/Article;class ArticleSearch extends Article //public $cname;//文章类别名 * @inheritdoc public function rules() return [ [[ cid , created_at , updated_at ], integer ], [[ id , desc , title , cover , content ], safe ], * @inheritdoc public function scenarios() // bypass scenarios() implementation in the parent class return Model::scenarios(); //搜索 public function search($params) $query = Article::find(); // $query- joinWith([ cate //关联文章类别表 // $query- joinWith([ author = function($query) { $query- from([ author = users }]); $dataProvider = new ActiveDataProvider([ query = $query, pagination = [ pageSize = 2, // 从参数的数据中加载过滤条件,并验证 $this- load($params); if (!$this- validate()) { // uncomment the following line if you do not want to any records when validation fails // $query- where( 0=1 return $dataProvider; // 增加过滤条件来调整查询对象 $query- andFilterWhere([ // cname = $this- cate.cname, title = $this- title, $query- andFilterWhere([ like , title , $this- title]); //$query- andFilterWhere([ like , cate.cname , $this- cname]) ; return $dataProvider;}
二、分页使用
方式一
首先在控制器的动作中,创建分页对象并且为其填充数据:
?php//other codeuse yii/data/Pagination;public function actionArticlelist() //分页读取类别数据 $model = Article::find()- with( cate $pagination = new Pagination([ defaultPageSize = 3, totalCount = $model- count(), $model = $model- orderBy( id ASC ) - offset($pagination- offset) - limit($pagination- limit) - all(); return $this- render( index , [ model = $model, pagination = $pagination,?
其次在视图中我们输出的模板为当前页并通过分页对象链接到该页:
?phpuse yii/widgets/LinkPager;use yii/helpers/Html;use yii/helpers/Url;//other codeforeach ($models as $model) { // 在这里显示 $model// 显示分页echo LinkPager::widget([ pagination = $pagination, firstPageLabel = First , prevPageLabel = Prev , nextPageLabel = Next , lastPageLabel = Last ,?
方式二
控制器:
?php $query = Article::find()- with( cate $provider = new ActiveDataProvider([ query = $query, pagination = [ pageSize = 3, sort = [ defaultOrder = [ // created_at = SORT_DESC, // title = SORT_ASC, return $this- render( index , [ model = $query, dataProvider = $provider?
视图:
?phpuse yii/grid/GridView;echo GridView::widget([ dataProvider = $dataProvider, //每列都有搜索框 控制器传过来$searchModel = new ArticleSearch(); // filterModel = $searchModel, layout = {items} p >三、搜索带分页功能
创建搜索模型(前面己做)
控制传入数据
视图显示控制器代码:
?phppublic function actionIndex() $searchModel = new ArticleSearch(); $dataProvider = $searchModel- search(Yii::$app- request- queryParams); return $this- render( index , [ searchModel = $searchModel, dataProvider = $dataProvider,?视图:
?php $form = ActiveForm::begin([ action = [ index ], method = get , id = cateadd-form , options = [ class = form-horizontal ],]); ? ?= $form- field($searchModel, title ,[ options = [ class = ], inputOptions = [ placeholder = 文章搜索 , class = input-sm form-control ],])- label(false) ? ?= Html::submitButton( Go! , [ class = btn btn-sm btn-primary ]) ? ?php ActiveForm::end(); ? ?= GridView::widget([ dataProvider = $dataProvider, layout = {items} p >以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP !
相关推荐:
关于YII框架中搜索分页jQuery写法
Yii使用CLinkPager进行的分页
以上就是yii2实现分页和带搜索的分页功能的详细内容,PHP教程
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
新闻热点
疑难解答