YII2.0 GridView配置

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
//         'tableOptions' => ['name'=>'tabalse'],//table 属性
        'filterRowOptions'=>['class' => 'filters'],//搜索框的class
        'headerRowOptions' => ['class'=>'abc'],//排序行的属性
        'id' => 'idss',//设置table外的div的id
        'options' => ['name' => 'options'],//设置table外的div的属性
        'pager' => [//分页样式调整
            'activePageCssClass'=>'active',
            'options' => ['class' => 'pagination'],
        ],
        'showFooter' => false,//是否显示tfoot
        'placeFooterAfterBody' => false,
        'rowOptions' => ['name'=>'rowOptionsss'],//给每个tr加属性
        'showHeader' => true,//是否显示表头(搜索行与th行)
        'layout' => "{summary}\n{items}\n{pager}",//板块//layout有5个值,分别为{summary}、{errors}、{items}、{sorter}和{pager}。
        'summary' => "{begin}-{end}-{count}-{totalCount}-{page}-{pageCount}",//数据的相关信息,行,页面,总数等
        'summaryOptions' => ['class' => 'summarys'],
        'afterRow' => function ()//每一行渲染后执行的方法
        {
//             return '<td>111</td>';
        },
        'beforeRow' => function ()//每一行渲染前执行的方法
        {
//             return '<td>222</td>';
        },
        'caption' => '表格的标题',//表格的标题
        'captionOptions' => ['class'=>'capClass'],//表格的标题的html标签属性设置
        'emptyCell' => '<p>null</p>',//单元格数据为空时显示的内容
        'emptyText' => 'emptyText',//$dataProvider为空时显示的内容
        'columns' => [//设置的列(字段)
//             ['class' => 'yii\grid\SerialColumn'],//序号列
            [
                'attribute'=>'id',
                'contentOptions'=>['width'=>'30px'],
            ],
            'title',
            [
                'attribute'=>'authorName',
                'label' => '作者',
                'value'=>'author.nickname',
            ],
//             'content:ntext',
            'tags:ntext',
            [
                'attribute'=>'status',
                'value'=>'status0.name',
                'filter'=>Poststatus::find()
                    ->select(['name', 'id'])
                    ->orderBy('position')
                    ->indexBy('id')
                    ->column(),
            ],
            //'create_time:datetime',
//             'update_time:datetime',
            [
                'label' => '修改时间',
                'attribute'=>'update_time',
                'value' => function ($data) {
                    return date('Y-m-d H:i:s',$data->update_time);
                },
            ],

            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{view}{update}{delete}{approve}',
                'buttons' =>[
                    'approve' => function ($url, $model, $key)
                    {
                        $options = [
                            'title' => Yii::t('yii', '审核'),
                            'arid-label' => Yii::t('yii', '审核'),
                            'data-confirm' => Yii::t('yii', '你确定要通过这条评论吗?'),
                            'data-method' => 'post',
                            'data-ajax' => '0',
                        ];
                        return Html::a('<span class="glyphicon glyphicon-check"></span>', $url, $options);
                    }
                ],
            ],
        ],
    ]); ?>

$this->redirect(['view', 'id'=>32]);//跳转到本controll中的其他方法
  Html类: /vendor/yiisoft/yii2/helpers/BaseHtml.php 参考手册->yii\helpers\BaseHtml
  ArrayHelper类:/vendor/yiisoft/yii2/helpers/BaseArrayHelper.php 参考手册->yii\helpers\ArrayHelper

数据查询:(三种方式)
  Command:(return Array)不常用,不推荐
    Yii::$app->db->createCommand('SELECT * FROM post')->queryAll();

    $post = Yii::$app->db->createCommand('SELECT * FROM post  where id=:id and status=:status')
      ->bindValue(':id', $id)
      ->bindValue(':status', 2)
      ->queryOne();
  AR:(return Object)常用,推荐;
    一个AR类关联一张数据表,每个AR对象对应表中的一行;
    AR对象的属性对应为数据行的列;
    可以直接以面向对象的方式来操纵数据库中的数据,这样就不需要写SQL语句就能实现数据库的访问
    Post::find():\yii\db\ActiveRecord类中的方法;该方法调用的ActiveQuery

    查询:
      $model = Post::find()->where(['id' => 32])->one();//Post::findOne(1);//主键为1的
      $model = Post::find()->where(['status' => 2])->all();//Post::findAll(['status'=>2]);
      $model = Post::find()->where(['AND',['status'=>2],['author_id'=>1],['like','title','yii']])->orderBy('id')->all();
    插入:
      $postMode = new Post();
      $postMode->title = 't';
      $postMode->content = 'contentcontentcontentcontent';
      $postMode->status = 1;
      $postMode->author_id = 1;
      $postMode->save();
    更新:
      $model = Post::findOne(32);
      $model->title = 'Yii2小部件详解32323232';
      $model->save();
    删除:
      $model = Post::findOne(32);
      $model->delete();

  findBySql:(属于AR类)
    $sql = 'SELECT * FROM post WHERE status=2';
    $post = Post::findBySql($sql);
  QueryBuilder:(查询构造器return Array)
    $rows = (new \yii\db\Query())
      ->select(['id', 'email'])
      ->from('user')
      ->where(['id'=>1])//->where("status=:status")->addParams([':status'=>$status])
      ->orderBy(id)
      ->limit(10)
      ->indexBy(id)//调用all的时候id的值做为数组的key,而不是默认为索引数组
      //->join('LEFT JOIN', 'post', 'post.user_id = user.id')
      ->all();

    $rows->union($rows2);//联合查询

posted @ 2018-10-31 12:18  llxpbbs  阅读(525)  评论(0编辑  收藏  举报