Explorar o código

feat: NG-15: 新会员注册时增加国家与语言选项.

kevin hai 1 ano
pai
achega
37dbc79f00
Modificáronse 2 ficheiros con 16 adicións e 13 borrados
  1. 5 0
      common/models/FreeTemplate.php
  2. 11 13
      common/models/forms/FreeTemplateForm.php

+ 5 - 0
common/models/FreeTemplate.php

@@ -109,4 +109,9 @@ class FreeTemplate extends \common\components\ActiveRecord
     {
         return self::findOneAsArray('country_id=:country_id', [':country_id' => $countryId]);
     }
+
+    public static function deleteOne(string $countryId): bool
+    {
+        return self::deleteAll('country_id=:country_id', [':country_id' => $countryId]);
+    }
 }

+ 11 - 13
common/models/forms/FreeTemplateForm.php

@@ -10,17 +10,12 @@ use yii\base\Exception;
 
 class FreeTemplateForm extends Model
 {
+    public $modelClass = FreeTemplate::class;
+
     public $countryId;
     public $freight;
     public $freeShipping;
     public $currencyId;
-    private $_model;
-
-    public function init() {
-        parent::init();
-
-        $this->_model = FreeTemplate::findOne(['country_id' => $this->countryId]) ?: new FreeTemplate();
-    }
 
     /**
      * @inheritdoc
@@ -78,14 +73,17 @@ class FreeTemplateForm extends Model
 
         $transaction = \Yii::$app->db->beginTransaction();
         try {
+            // 删除旧数据
+            $this->modelClass::deleteOne($this->countryId);
             // 添加/修改数据
-            $this->_model->country_id = $this->countryId;
-            $this->_model->freight = $this->freight;
-            $this->_model->free_shipping = $this->freeShipping;
-            $this->_model->currency_id = $this->currencyId;
-            if (!$this->_model->save()) {
+            $model = new FreeTemplate();
+            $model->country_id = $this->countryId;
+            $model->freight = $this->freight;
+            $model->free_shipping = $this->freeShipping;
+            $model->currency_id = $this->currencyId;
+            if (!$model->save()) {
                 $transaction->rollBack();
-                throw new Exception(Form::formatErrorsForApi($this->_model->getErrors()));
+                throw new Exception(Form::formatErrorsForApi($model->getErrors()));
             }
 
             $transaction->commit();