一、模型配置
事例会用到三个models。文章类别表和文章表用gii生成下即可,最后一个是搜索验证模型。其中,只讲下一个联表和搜索验证。其他不用操作。
1.文章表关联
2.搜索模型
common/models/search/创建ArticleSearch.PHP
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;
}
// <a href="/tag/zengjia/" target="_blank" class="keywords">增加</a>过滤条件来调整<a href="/tag/chaxun/" target="_blank" class="keywords">查询</a>对象
$query->andFilterWhere([
// 'cname' => $this->cate.cname,'title' => $this->title,]);
$query->andFilterWhere(['like',$this->title]);
//$query->andFilterWhere(['like','cate.cname',$this->cname]) ;
return $dataProvider;
}
}
二、分页使用
方式一
首先在控制器的动作中,创建分页对象并且为其填充数据:
$model = $model->orderBy('id ASC')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render('index',[
'model' => $model,'pagination' => $pagination,]);
}
?>
其次在视图中我们输出的模板为当前页并通过分页对象链接到该页:
echo LinkPager::widget([
'pagination' => $pagination,'firstPageLabel'=>"First",'prevPageLabel'=>'Prev','nextPageLabel'=>'Next','lastPageLabel'=>'Last',]);
?>
方式二
控制器:
$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
]);
?>
视图:
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"> 删除',['artdel','id' => $key],['class' => 'btn btn-default btn-xs','data' => ['confirm' => '你确定要删除文章吗?',]
]);
},'update' => function($url,$key){
return Html::a('<i class="fa fa-file"> 更新',['artedit',['class' => 'btn btn-default btn-xs']);
},]);
?>
三、搜索带分页功能
return $this->render('index',[
'searchModel' => $searchModel,'dataProvider' => $dataProvider,]);
}
?>
视图:
<?= $form->field($searchModel,[
'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,'pager'=>[
//'options'=>['class'=>'hidden']//关闭自带分页
'firstPageLabel'=>"First",//这部分和上面的分页是一样的