Example #1
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $productTags = isset($_REQUEST['tagsArray']) && is_array($_REQUEST['tagsArray']) ? $_REQUEST['tagsArray'] : array();
     $model = new Product();
     // aggiorno la data di creazione ogni volta
     $now = new DateTime();
     $model->timestamp = $now->format('Y-m-d H-i-s');
     if (isset($_POST['Product'])) {
         if (count($productTags) <= 0) {
             $model->addError('', 'At least one tag is required');
         } else {
             $model->attributes = $_POST['Product'];
             $file = CUploadedFile::getInstance($model, 'image');
             if ($file != null) {
                 $model->image = uniqid('product_') . '.' . $file->getExtensionName();
                 if ($model->save()) {
                     $model->tagsArray = $productTags;
                     $file->saveAs($model->imagePath);
                     $this->redirect(array('view', 'id' => $model->id));
                 }
             } else {
                 $model->addError('image', 'Required');
             }
         }
     }
     $this->render('create', array('model' => $model));
 }