/**
  * 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())) {
         //echo "<pre>"; print_r($model); die;
         if (isset($model->file)) {
             $model->file = UploadedFile::getInstance($model, 'file');
             //print_r($this->image); exit('gfdgf');
             if (is_object($model->file)) {
                 $name = \Yii::$app->basePath . "/web/images/";
                 //exit($name);
                 $random = Yii::$app->getSecurity()->generateRandomString(5);
                 $model->file->saveAs($name . $random . "_" . $model->file->baseName . '.' . $model->file->extension);
                 $model->file = $random . "_" . $model->file->baseName . '.' . $model->file->extension;
             }
         }
         $model->save();
         return $this->redirect(['view', 'id' => (string) $model->_id]);
     } else {
         $js = '$("#modal").modal("show")';
         $this->getView()->registerJs($js);
         // return $this->render('index');
         return $this->render('create', ['model' => $model]);
     }
 }
 /**
  * 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();
     $model->user_id = Yii::$app->user->id;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemple #3
0
 public function actionCreateArticle()
 {
     $model = new Article();
     $dataModel = new ArticleData();
     if ($model->load(\Yii::$app->request->post()) && $dataModel->load(\Yii::$app->request->post())) {
         $isValid = $model->validate();
         if ($isValid) {
             $model->save(false);
             $dataModel->id = $model->id;
             $isValid = $dataModel->validate();
             if ($isValid) {
                 $dataModel->save(false);
                 \Yii::$app->session->setFlash('success', '投稿成功,请等待管理员审核!');
                 return $this->redirect(['create-article']);
             }
         }
     }
     return $this->render('create-article', ['model' => $model, 'dataModel' => $dataModel]);
 }