| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace addons\OperatingNote\common\models;
- use common\enums\StatusEnum;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_operating_note_cate}}".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property string $name 分类名/不超过6个字符
- * @property int $status -1是删除,0是禁用,1是启用
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- */
- class AddonsOperatingNoteCate extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_operating_note_cate}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'status'], 'required'],
- [['mall_id', 'status', 'created_at', 'updated_at', 'sort'], 'integer'],
- [['name'], 'string', 'max' => 50],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'name' => '分类名/不超过6个字符',
- 'sort' => '排序默认为0',
- 'status' => '-1是删除,0是禁用,1是启用',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * User: wniu
- * Date: 2022/2/17
- * Time: 4:40 下午
- * * @param array $params
- * * @return bool
- */
- public static function setData(array $params): bool
- {
- try {
- if (isset($params['id'])) {
- $model = self::getOne([['id' => $params['id']]]);
- if (!$model) {
- self::$static_error = '该分类不存在';
- return false;
- }
- if($params['status'] == StatusEnum::DELETE){
- $has_content = AddonsOperatingNoteContent::count(['where' => [['cate_id' => $params['id'], 'status' => StatusEnum::ENABLED]]]);
- if ($has_content > 0) {
- self::$static_error = '该分类有关联文章';
- return false;
- }
- }
- } else {
- $model = self::getOne([
- ['name' => $params['name']],
- ['status'=>StatusEnum::ENABLED],
- ['mall_id' => $params['mall_id']
- ]]);
- if ($model) {
- self::$static_error = '已存在相同名称分类';
- return false;
- }
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '') || !$model->save()) {
- throw new Exception($model->getErrorMessage());
- }
- return true;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|