コード例 #1
0
 public function actionCreate()
 {
     $model = new Article();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #2
0
ファイル: DefaultController.php プロジェクト: obedkin/atlant
 /**
  * Creates a new Article model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Article();
     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 = 'article_' . $id . '.' . $model->file->extension;
             $model->file->saveAs(Yii::$app->params['article']['image']['path'] . $image_name);
             $model->image = $image_name;
             $model->save();
         }
         return $this->redirect(['view', 'id' => $id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }