Browse Source

refactor:重构规格创建和编辑方法

antshop
linyaostalker 5 years ago
parent
commit
e0f21480a8
  1. 19
      backend/modules/goods/controllers/AttributeController.php
  2. 20
      backend/modules/goods/logic/goods/GoodsManager.php

19
backend/modules/goods/controllers/AttributeController.php

@ -2,6 +2,7 @@
namespace backend\modules\goods\controllers;
use backend\modules\goods\logic\goods\GoodsManager;
use Yii;
use backend\modules\goods\models\ars\Attribute;
use backend\modules\goods\models\searchs\AttributeSearch;
@ -71,14 +72,7 @@ class AttributeController extends Controller
$model->cat_id = 0;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$value = $model->value;
$model->value = str_replace(',', ',', $value);
$array = explode(',', $model->value);
if (count($array) != count(array_unique($array))) {
Yii::$app->getSession()->setFlash('error', '不能有相同的属性值');
return $this->render('create', ['model' => $model]);
}
if ($model->save()) {
if (GoodsManager::updateAttribute($model)) {
return $this->redirect(['index']);
}
}
@ -100,16 +94,9 @@ class AttributeController extends Controller
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->value = str_replace(',', ',', $model->value);
$array = explode(',', $model->value);
if (count($array) != count(array_unique($array))) {
Yii::$app->getSession()->setFlash('error', '不能有相同的属性值');
return $this->render('create', ['model' => $model]);
}
if ($model->save()) {
if (GoodsManager::updateAttribute($model)) {
return $this->redirect(['index']);
}
return $this->redirect('index');
}
return $this->render('update', [

20
backend/modules/goods/logic/goods/GoodsManager.php

@ -557,4 +557,24 @@ class GoodsManager
}
return false; //否则返回false,表示后台分类可以修改
}
/**
* @param Attribute|$attrModel
* @return bool
* 编辑规格属性
*/
public static function updateAttribute($attrModel)
{
$attrModel->value = str_replace(',', ',', $attrModel->value);
$attrValue = explode(',', $attrModel->value);
if (count($attrValue) != count(array_unique($attrValue))) {
Yii::$app->getSession()->setFlash('error', '不能有相同的属性值');
return false;
}
if (!$attrModel->save()) {
Yii::$app->getSession()->setFlash('error', '保存失败');
return false;
}
return true;
}
}
Loading…
Cancel
Save