コード例 #1
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Posts();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $model->date = strtotime($model->strDate);
         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #2
0
 public function actionBlog($id = null)
 {
     $user = Yii::$app->user->id;
     /** @var $profile \app\models\Profile */
     if ($id === null) {
         $profile = Profile::findOne(['user_id' => $user]);
         $queryPosts = Posts::find()->where(['user_id' => $user]);
     } else {
         $queryPosts = Posts::find()->where(['user_id' => $id]);
         $profile = Profile::findOne(['user_id' => $id]);
         if (!isset($profile)) {
             Yii::$app->getSession()->setFlash('error', 'that profile not found');
             return $this->goHome();
         }
     }
     $pagination = new Pagination(['defaultPageSize' => 5, 'totalCount' => $queryPosts->count()]);
     $posts = $queryPosts->orderBy(['id' => SORT_DESC])->offset($pagination->offset)->limit($pagination->limit)->all();
     $model = new Posts();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         if ($model->createPost()) {
             $this->refresh();
         } else {
             $errors = $model->errors;
             Yii::$app->session->setFlash('error', 'Error: check the fields');
         }
     }
     return $this->render('blog', ['posts' => $posts, 'profile' => $profile, 'activePage' => Yii::$app->request->get('page', 1), 'countPages' => $pagination->getPageCount(), 'pagination' => $pagination, 'model' => $model]);
 }