/**
  * Creates a new CharacteristicValueModel model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new CharacteristicValueModel();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'idProduct' => $model->idProduct, 'idCharacteristic' => $model->idCharacteristic]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemplo n.º 2
0
 /**
  * Updates an existing ProductModel model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     if (isset($_FILES['ProductModel']) && $_FILES['ProductModel']['name']['photo'] != "") {
         if (!in_array($_FILES['ProductModel']['type']['photo'], $this->image_array)) {
             $model->addError('photo', 'Avaliable file types: jpg, gif, png.');
         } else {
             if ($model->photo != "") {
                 unlink(Yii::getAlias('@app') . Yii::getAlias('@web') . '/' . $model->photo);
             }
             $rnd = rand(0, 9999);
             $uploadedFile = UploadedFile::getInstance($model, 'photo');
             $fileName = 'files/' . $rnd . '_' . $uploadedFile->name;
             $model->photo = $fileName;
             $uploadedFile->saveAs($fileName);
         }
     }
     if (isset($_POST['CharacteristicValueModel'])) {
         $characteristics_values = $_POST['CharacteristicValueModel'];
         $success_values = true;
         foreach ($characteristics_values as $characteristics_value) {
             $temp_value = CharacteristicValueModel::find()->where(['idProduct' => $characteristics_value['idProduct'], 'idCharacteristic' => $characteristics_value['idCharacteristic']])->one();
             if ($temp_value == null) {
                 $temp_value = new CharacteristicValueModel();
                 $temp_value->idProduct = $characteristics_value['idProduct'];
                 $temp_value->idCharacteristic = $characteristics_value['idCharacteristic'];
             }
             $temp_value->value = $characteristics_value['value'];
             if (!$temp_value->save()) {
                 print_r($temp_value->getErrors());
                 $success_values = false;
             }
         }
     }
     if ($model->load(Yii::$app->request->post()) && $model->save() && $success_values) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('update', ['model' => $model]);
     }
 }