TimestampBehavior::class, 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'], ] ] ]; } public static function setDefault($id, $store_id, $md_id) { if(!$store_id || !$id){ \Yii::error([__METHOD__, $id, $store_id]); throw new \Exception('参数错误.'); } $model = self::findOne($id); if($model->is_default){ $model->is_default = 0; if(!$model->save()){ \Yii::error([__METHOD__, func_get_args(), $model]); throw new \Exception(array_shift($model->getFirstErrors())); } }else{ self::updateAll(['is_default' => 0], ['and', ['store_id' => $store_id], ['md_id' => $md_id], ['!=', 'id', $id]]); self::updateAll(['is_default' => 1], ['id' => $id, 'store_id' => $store_id]); } return true; } public static function saveOne($store_id, $name, $desc = '', $cover_pic = '', $attr = [], $id = 0, $md_id = 0) { if(!$name){ \Yii::error([__METHOD__, func_get_args()]); throw new \Exception('参数错误.'); } $model = $id ? self::findOne($id) : new self(); $model->store_id = $store_id; $model->name = $name; $model->desc = $desc; $model->cover_pic = $cover_pic; foreach ($attr as &$item) { $item['price'] = !empty($item['price']) ? $item['price'] : 0; } $model->attr = json_encode($attr); $model->md_id = $md_id; if(!$model->save()){ \Yii::error([__METHOD__, func_get_args(), $model]); throw new \Exception(array_shift($model->getFirstErrors())); } return true; } public static function del($id, $store_id) { if(!$store_id || !$id){ \Yii::error([__METHOD__, $id, $store_id]); throw new \Exception('参数错误.'); } $model = self::findOne(['id' => $id, 'store_id' => $store_id]); $model->is_delete = 1; if(!$model->save()){ \Yii::error([__METHOD__, func_get_args(), $model]); throw new \Exception(array_shift($model->getFirstErrors())); } return true; } public static function list($params, $store_id) { $query = self::find()->where(['is_delete' => 0])->orderBy('id DESC'); $store_id && $query->andWhere(['store_id' => $store_id]); $params['name'] && $query->andWhere(['like', 'name', $params['name']]); $query->andWhere(['md_id' => $params['md_id']]); return pagination_make($query); } public static function getAttr($id, $store_id, $md_id, &$model = []) { if($id){ $model = self::findOne(['id' => $id]); }else{ $model = self::findOne(['store_id' => $store_id, 'is_default' => 1, 'md_id' => $md_id]); } return $model->attr; } }