View Layer INDEX.PHG Code:
<?PHP Useyii\helpers\html; UseYii\grid\gridview; UseYii\widgets\pjax; UseFrontend\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;? ><divclass= "Item-index" > $this->title)?>//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 '],//check box column display [' Class ' = ' Yii\grid\serialcolumn '],[//According to cate_id in the static method of model query category Cate_name display' attribute ' = ' cate_id ', ' value ' =function($model){ returnItem::get_type_text ($model-cate_id); },//' filter ' = is a drop-down list filter' Filter ' = Item::get_type (), ], ' name ', ' Buy_price ', ' Sell_price ', [ ' attribute ' = ' created_at ', ' Format ' =>[' Date ', ' php:y-m-d h:m:s '],//date format display ], [ ' attribute ' = ' updated_at ', ' Format ' =>[' Date ', ' php:y-m-d h:m:s '], ], [ ' attribute ' = ' status ', ' value ' =function($model){ return $model->status = = 1? ' Selling ': ' Discontinued ';//Determine the status data given by the model, for 1 is for sale}, ' filter ' = [' discontinued ', ' Sale '],//dropdown filter box ],//' Img_url:url ', [ ' Class ' = ' yii\grid\actioncolumn ',//Operation column ], ], ]); ?> <?php Pjax::End();?></div>
Model Layer Code:
<?phpnamespace frontend\models; UseYii; Usefrontend\models\category;/** * * @property string $id * @property integer $cate _id * @property string $name * @property double $buy _price * @prop Erty Double $sell _price * @property integer $created _at * @property integer $updated _at * @property integer $status * @pro Perty string $img _url*/classItemextends\yii\db\activerecord{ Public Static $category;//Use static variable categoryfunction__construct () {Parent::__construct (); if(Is_null(Self::$category) {//single mode, only one category is generated. Self::$category=category::find ()->select ([' cate_id ', ' cate_name '])All (); } } /** * @inheritdoc*/ Public Static functionTableName () {return' Item '; } /** * @inheritdoc*/ Public functionrules () {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 functionAttributelabels () {return [ ' id ' = ' + ' ' number ', ' cate_id ' = ' + ' category ', ' name ' ' = ' and ' name ', ' buy_price ' = ' purchase price (HKD) ', ' Sell_price ' = ' Sales price (CNY) ', ' created_at ' = ' creation time ', ' updated_at ' = ' Update Time ', ' Status ' = ' state ', ' img_url ' = ' img URL ', ]; } /** * @param unknown $id * @return ambigous <unknown>*/ Public Static functionGet_type_text ($id){ /**array_column (Array,column_key,index_key); The *php function Array_column (array,column_key,index_key) * replaces the (use) Yii\helpers\arrayhelper::map (); */ $datas=array_column (self::$category, ' Cate_name ', ' cate_id '); return $datas[$id]; }
Generate drop-down filter box, select which future key value will be returned to the query, the key value is the item's cate_id
Public Static functionGet_type () {$cat= Array_column (self::$category, ' Cate_name ', ' cate_id '); return $cat; }}
The code for the Itemsearch layer does not need to be modified.
Summary of MVC views making item and category