コード例 #1
0
 public function actionCreate()
 {
     $model = new PriceType();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['update', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #2
0
 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]);
     }
 }
コード例 #3
0
 public function search($params)
 {
     $query = PriceType::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
コード例 #4
0
ファイル: Product.php プロジェクト: pistol88/yii2-shop
 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;
 }