Ejemplo n.º 1
0
 /**
  * Creates a new ProductModel model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new ProductModel();
     $model->rating = 0;
     $model->amountRated = 0;
     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 {
             $rnd = rand(0, 9999);
             $uploadedFile = UploadedFile::getInstance($model, 'photo');
             $fileName = 'files/' . $rnd . '_' . $uploadedFile->name;
             $model->photo = $fileName;
             $uploadedFile->saveAs($fileName);
         }
     }
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }