制作item和category的mvc视图总结

View层index.phg 代码:

<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use frontend\models\Item;
/* @var $this yii\web\View */
/* @var $searchModel frontend\models\ItemSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Items';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="item-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php Pjax::begin(); ?>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('Create Item', ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class'=>'yii\grid\CheckboxColumn'],//复选框列显示
            ['class' => 'yii\grid\SerialColumn'],
            [   //根据cate_id在model的静态方法里面查询category的cate_name显示
                'attribute' =>'cate_id',
                'value' => function($model){
                    return Item::get_type_text($model->cate_id);
                },
                //'filter' =>是下拉列表过滤器
                'filter' => Item::get_type(),
            ],
            'name',
            'buy_price',
            'sell_price',
            [
                'attribute' => 'created_at',
                'format'=>['date','php:Y-m-d H:m:s'],//日期格式显示
            ],
            [
                'attribute' => 'updated_at',
                'format'=>['date','php:Y-m-d H:m:s'],
            ],
            [
                'attribute' => 'status',
                'value' => function($model){
                    return $model->status == 1?'在售':'停售';//判断model给的status数据,为1时候是在售
                },
                'filter' => ['停售','在售'],//下拉筛选框
            ],
            // 'img_url:url',

            [
            'class' => 'yii\grid\ActionColumn',//操作列

            ],
        ],
    ]); ?>
    <?php Pjax::end(); ?>
</div>

model层代码:

<?php

namespace frontend\models;

use Yii;
use frontend\models\Category;
/**
 *
 * @property string $id
 * @property integer $cate_id
 * @property string $name
 * @property double $buy_price
 * @property double $sell_price
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $status
 * @property string $img_url
 */
class Item extends \yii\db\ActiveRecord
{

    public static $category;//使用了静态变量category

    function __construct() {

       parent::__construct();

       if(is_null(SELF::$category)){//单一模式,只生成一个category。

        SELF::$category=Category::find()->select(['cate_id','cate_name'])->all();

       }
   }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'item';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['cate_id', 'created_at', 'updated_at', 'status'], 'integer'],
            [['buy_price', 'sell_price'], 'number'],
            [['created_at', 'updated_at'], 'required'],
            [['name'], 'string', 'max' => 100],
            [['img_url'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '编号',
            'cate_id' => '类别',
            'name' => '品名',
            'buy_price' => '采购价(HKD)',
            'sell_price' => '销售价(CNY)',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'status' => '状态',
            'img_url' => 'Img Url',
        ];
    }

    /**
    * 通过栏目id获得栏目名称
    * @param unknown $id
    * @return Ambigous <unknown>
    */
    public static function  get_type_text($id){

        /*
        *array_column(array,column_key,index_key);
        *php自带函数array_column(array,column_key,index_key)
        *替换了(use) Yii\helpers\ArrayHelper::map();
        */
        $datas =array_column(SELF::$category,'cate_name','cate_id');

        return  $datas[$id];
    }

  //生成下拉筛选框,选择哪个以后key值会返给查询,key值就是item的cate_id
public static function get_type(){ $cat = array_column(SELF::$category,'cate_name','cate_id'); return $cat; } }

itemSearch 层的代码不需要修改。

posted @ 2017-02-18 00:06  jerryhe326  阅读(327)  评论(0编辑  收藏  举报