|
|
@@ -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();
|