Exemplo n.º 1
0
 /**
  * Creates a new News model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new News();
     if ($model->load(Yii::$app->request->post())) {
         $model->image = 'default';
         $model->save();
         $id = $model->id;
         //Если пришла картинка в POST-запросе
         $model->load(Yii::$app->request->post());
         $model->file = UploadedFile::getInstance($model, 'file');
         if ($model->file && $model->validate()) {
             $image_name = 'news_' . $id . '.' . $model->file->extension;
             $model->file->saveAs(Yii::$app->params['news']['image']['path'] . $image_name);
             $model->image = $image_name;
             $model->save();
         }
         return $this->redirect(['view', 'id' => $id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemplo n.º 2
0
 /**
  * Creates a new News model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new News();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }