一、模型配置
案例會用到三個models。文章類別表和文章表用gii產生下即可,最後一個是搜尋驗證模型。其中,只講下一個聯表和搜尋驗證。其他不用操作。
1.文章表關聯
<?php//...other code//關聯public function getCate(){ return $this->hasOne(ArticleCate::className(),['id' => 'cid']); }?>
2.搜尋模型
common/models/search/建立ArticleSearch.php
<?php namespace 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}<div class="text-right tooltip-demo">{pager}</div>', 'pager'=>[ //'options'=>['class'=>'hidden']//關閉內建分頁 'firstPageLabel'=>"First", 'prevPageLabel'=>'Prev', 'nextPageLabel'=>'Next', 'lastPageLabel'=>'Last', ], 'columns' => [ //['class' => 'yii\grid\SerialColumn'],//序號從1開始 // 資料提供者中所含資料所定義的簡單的列 // 使用的是模型的列的資料 'id', 'username', ['label'=>'文章類別', /*'attribute' => 'cid',產生一個a標籤,點擊可排序*/ 'value' => 'cate.cname' ], ['label'=>'發布日期','format' => ['date', 'php:Y-m-d'],'value' => 'created_at'], // 更複雜的列資料 ['label'=>'封面圖','format'=>'raw','value'=>function($m){ return Html::img($m->cover,['class' => 'img-circle','width' => 30]); }], [ 'class' => 'yii\grid\DataColumn', //由於是預設類型,可以省略 'value' => function ($data) { return $data->name; // 如果是數組資料則為 $data['name'] ,例如,使用 SqlDataProvider 的情形。 }, ], [ 'class' => 'yii\grid\ActionColumn', 'header' => '操作', 'template' => '{delete} {update}',//只需要展示刪除和更新 /*'headerOptions' => ['width' => '80'],*/ 'buttons' => [ 'delete' => function($url, $model, $key){ return Html::a('<i class="glyphicon glyphicon-trash"></i> 刪除', ['artdel', 'id' => $key], ['class' => 'btn btn-default btn-xs', 'data' => ['confirm' => '你確定要刪除文章嗎?',] ]); }, 'update' => function($url, $model, $key){ return Html::a('<i class="fa fa-file"></i> 更新', ['artedit', 'id' => $key], ['class' => 'btn btn-default btn-xs']); }, ], ], ],]);?>
三、搜尋帶分頁功能
建立搜尋模型(前面己做)
控制傳入資料
視圖顯示控制器代碼:
<?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}<div class="text-right tooltip-demo">{pager}</div>', 'pager'=>[ //'options'=>['class'=>'hidden']//關閉內建分頁 'firstPageLabel'=>"First", 'prevPageLabel'=>'Prev', 'nextPageLabel'=>'Next', 'lastPageLabel'=>'Last', ], //這部分和上面的分頁是一樣的
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援topic.alibabacloud.com。