/** * Lists all PostComment models on specific post type. * If there is post_id the action will generate list of all PostComment models based on post_id. * * @param integer $post_type * @param null|integer $post_id * * @throws \yii\web\NotFoundHttpException * @return string */ public function actionIndex($post_type, $post_id = null) { $post = null; $postType = $this->findPostType($post_type); if ($post_id) { $post = $this->findPost($post_id); } $searchModel = new PostCommentSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $post_type, $post_id); return $this->render('index', ['post' => $post, 'postType' => $postType, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @param int $posttype Post type ID * @param int|null $post Post ID * @return ActiveDataProvider */ public function search($params, $posttype, $post = null) { $query = PostCommentModel::find(); $query->innerJoinWith(['commentPost' => function ($query) { /* @var $query \yii\db\ActiveQuery */ return $query->from(['post' => Post::tableName()]); }])->from(['postComment' => PostComment::tableName()]); $query->andWhere(['post.type' => $posttype]); if ($post) { $query->andWhere(['post.id' => $post]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'post_id' => $this->post_id, 'parent' => $this->parent, 'user_id' => $this->user_id]); $query->andFilterWhere(['like', 'postComment.author', $this->author])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'url', $this->url])->andFilterWhere(['like', 'ip', $this->ip])->andFilterWhere(['like', 'postComment.content', $this->content])->andFilterWhere(['like', 'postComment.status', $this->status])->andFilterWhere(['like', 'agent', $this->agent])->andFilterWhere(['like', 'postComment.date', $this->date])->andFilterWhere(['like', 'post.title', $this->post_title]); return $dataProvider; }