'否', self::IS_TAKING_YES => '是' ]; public static $isExpress = [ self::IS_EXPRESS_NO => '否', self::IS_EXPRESS_YES => '是' ]; public static $expressType = [ self::EXPRESS_TYPE_UNIFORM_POSTAGE => '统一邮费', self::EXPRESS_TYPE_EXPRESS_TEMPLATE => '运费模板' ]; public static $isSale = [ self::IS_SALE_NO => '不在售', self::IS_SALE_YES => '在售' ]; public $ruleVerify = 0;//规则验证是否通过 /** * {@inheritdoc} */ public static function tableName() { return 'atg_goods'; } /** * {@inheritdoc} */ public function rules() { return [ [['pid', 'cat_id', 'brand_id', 'shop_cat_id', 'supplier_id', 'weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'limit_count', 'stock', 'stock_warn', 'image', 'model_id', 'is_sale', 'sort_order', 'bouns_points', 'experience_points', 'is_delete', 'express_template', 'sku_mode', 'is_taking', 'is_express', 'express_type'], 'integer'], [['cat_id', 'brand_id', 'shop_cat_id', 'name'], 'required'], [['sn'], 'checkExist'], [['description', 'coverImageId', 'detailImageId'], 'string'], [['name'], 'string', 'max' => 120], [['sn'], 'string', 'max' => 60], [['code'], 'string', 'max' => 50], [['unit'], 'string', 'max' => 16], [['brief'], 'string', 'max' => 255], [['weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'market_price', 'price', 'uniform_postage'], 'checkNegative'], [['uniform_postage', 'market_price', 'price'], 'safe'] ]; } /** * @param $attribute * @param $params * 验证是否为负数 */ public function checkNegative($attribute, $params) { if ($this->$attribute < 0) { $this->addError($attribute, "不得为负数"); } } /** * @param $attribute * @param $params * 验证商品编号唯一 */ public function checkExist($attribute, $params) { $goods = self::find()->where([$attribute => $this->$attribute, 'is_delete' => 0])->one(); if ($this->isNewRecord) { if ($goods) { $this->addError($attribute, "该商品编号已经存在"); } } else { if ($goods && $goods->id != $this->id) { $this->addError($attribute, "该商品编号已经存在"); } } } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'id', 'pid' => '父级id', 'cat_id' => '后台商品类别', 'brand_id' => '品牌', 'shop_cat_id' => '前端商品类别', 'name' => '商品名称', 'sn' => '商品唯一货号', 'code' => '商品货码', 'supplier_id' => '供应商', 'weight' => '重量', 'length' => '长度', 'width' => '宽度', 'height' => '高度', 'diameter' => '直径', 'unit' => '单位', 'sold_count' => '总销量', 'limit_count' => '限购数量', 'stock' => '库存', 'stock_warn' => '库存警告', 'market_price' => '市场价', 'price' => '销售价', 'brief' => '简介', 'description' => '详细介绍', 'image' => '首页图片', 'model_id' => '模型id', 'is_sale' => '销售状态', 'sort_order' => '排序', 'bouns_points' => '奖励积分', 'experience_points' => '经验值', 'is_delete' => '是否删除,1为已删除', 'express_template' => '配送详情', 'created_at' => '创建时间', 'updated_at' => '更新时间', 'sku_mode' => 'sku类型', 'is_taking' => '是否自提', 'is_express' => '是否快递发货', 'express_type' => '快递运费方式', 'uniform_postage' => '统一邮费', ]; } /** * @author linyao * @email 602604991@qq.com * @created Nov 8, 2019 * * 行为存储创建时间和更新时间 */ public function behaviors() { return [ [ 'class' => TimestampBehavior::className(), 'createdAtAttribute' => 'created_at', 'updatedAtAttribute' => 'updated_at', 'value' => function() { return time(); }, ], ]; } /** * @param bool $insert * @return bool * 自动填入参数 */ public function beforeSave($insert) { if (!$this->sn) { $this->sn = time() . rand(1111, 9999); } if (!$this->limit_count) { $this->limit_count = 0; } return parent::beforeSave($insert); // TODO: Change the autogenerated stub } public function getCategory() { return $this->hasOne(Category::className(), ['id' => 'cat_id']); } public function getShopCategory() { return $this->hasOne(ShopCategory::className(), ['id' => 'shop_cat_id']); } public function getImageFile() { return $this->hasOne(File::className(), ['id' => 'image']); } public function getBrand() { return $this->hasOne(Brand::className(), ['id' => 'brand_id']); } public function getSupplier() { return $this->hasOne(Supplier::className(), ['id' => 'supplier_id']); } }