Exemple #1
0
 /**
  * app\modules\home\models\Post
  * @return array
  */
 public function getPosts()
 {
     //return $this->hasMany(Post::className(), ['user_id' => 'id'])->orderBy('id DESC');
     $query = Post::find()->where(['user_id' => $this->id])->orderBy('id desc');
     $countQuery = clone $query;
     $pages = new \yii\data\Pagination(['totalCount' => $countQuery->count()]);
     $pages->defaultPageSize = 14;
     $posts = $query->offset($pages->offset)->limit($pages->limit)->all();
     return ['posts' => $posts, 'pages' => $pages];
 }
 public function actionViewPost($id)
 {
     if (($model = Post::findOne($id)) === null) {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
     return $this->render('/user/viewPost', ['model' => $model]);
 }
Exemple #3
0
 /**
  * Finds the Post model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Post the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Post::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }