Exemplo n.º 1
0
 /**
  * Creates a new CommentModel model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new CommentModel();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         $product = ProductModel::find()->where(['id' => $model->idProduct])->one();
         $product->rating = ($product->rating * $product->amountRated + $model->mark) / ($product->amountRated + 1);
         $product->amountRated = $product->amountRated + 1;
         $product->save();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemplo n.º 2
0
 public function actionAdd()
 {
     $model = new CommentModel();
     $model->date = date("Y-m-d");
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         $product = ProductModel::find()->where(['id' => $model->idProduct])->one();
         $product->rating = ($product->rating * $product->amountRated + $model->mark) / ($product->amountRated + 1);
         $product->amountRated = $product->amountRated + 1;
         if (!$product->save()) {
             echo "Error updating product.";
         }
     } else {
         echo "Error adding comment.";
     }
     return $this->redirect(\Yii::$app->user->getReturnUrl());
 }