- 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
- */
- classItemextends \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 functionget_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;
- }
- }
来源: http://www.bubuko.com/infodetail-1948975.html