public function actionCreate() { $model = $this->module->getService('product'); $priceModel = $this->module->getService('price'); $priceTypes = PriceType::find()->orderBy('sort DESC')->all(); if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($prices = yii::$app->request->post('Price')) { foreach ($prices as $typeId => $price) { $type = PriceType::findOne($typeId); $price = new $priceModel($price); $price->type_id = $typeId; $price->name = $type->name; $price->sort = $type->sort; $price->product_id = $model->id; $price->save(); } } $module = $this->module; $productEvent = new ProductEvent(['model' => $model]); $this->module->trigger($module::EVENT_PRODUCT_CREATE, $productEvent); return $this->redirect(['update', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'priceModel' => $priceModel, 'priceTypes' => $priceTypes]); } }
public function setPrice($price, $type = null) { if ($priceModel = $this->getPriceModel($type)) { $priceModel->price = $price; return $priceModel->save(false); } else { if ($typeModel = PriceType::findOne($type)) { $priceModel = new Price(); $priceModel->product_id = $this->id; $priceModel->price = $price; $priceModel->type_id = $type; $priceModel->name = $typeModel->name; return $priceModel->save(); } } return false; }