public function actionView($id) { $model = Post::findOne(['id' => $id, 'status' => 1]); if ($model !== null) { return $this->render('view', ['model' => $model]); } else { throw new NotFoundHttpException('The requested post does not exist.'); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Post::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'category_id' => $this->category_id, 'created_by' => $this->created_by, 'created_date' => $this->created_date, 'publish_date' => $this->publish_date, 'updated_date' => $this->updated_date, 'views' => $this->views, 'active' => $this->active]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'alias', $this->alias])->andFilterWhere(['like', 'desciption', $this->desciption])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'meta_title', $this->meta_title])->andFilterWhere(['like', 'meta_desciption', $this->meta_desciption]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Post::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['created_at' => '']], 'pagination' => ['pageSize' => 10]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'main' => $this->main, 'cat_id' => $this->cat_id, 'FROM_UNIXTIME(created_at, "%d.%m.%Y")' => $this->created_at]); $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'title', $this->title]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * @param integer $pageSize The number of results to be displayed per page. * @param boolean $published Whether or not posts have to be published. * * @return ActiveDataProvider */ public function search($params, $pageSize = 3, $published = false) { $query = Post::find(); // this means that editor is trying to see posts // we will allow him to see published ones and drafts made by him if ($published === true) { $query->where(['status' => Post::STATUS_PUBLISHED]); $query->orWhere(['user_id' => Yii::$app->user->id]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]], 'pagination' => ['pageSize' => $pageSize]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'status' => $this->status, 'type' => $this->type]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'content', $this->content]); return $dataProvider; }
public function actionView() { $id = \Yii::$app->request->get('id', 0); $alias = \Yii::$app->request->get('alias', 'non-alias'); Post::updateAllCounters(['views' => 1], ['id' => $id]); $cache = \Yii::$app->getCache(); $cache->keyPrefix = 'post-'; $data = $cache->get('post_' . $id); if ($data === false) { $post = Post::findOne(['id' => $id, 'alias' => $alias]); if ($post) { $data = $post; $cache->set('post_' . $id, $data); } else { throw new NotFoundHttpException('Post not found'); } } //var_dump($data);die(); \Yii::$app->view->title = $data->title; return $this->render('view', ['post' => $data]); }
protected function findModel($id) { if (is_array($id)) { $model = Post::findAll($id); } else { $model = Post::findOne($id); } if ($model !== null) { return $model; } else { throw new HttpException(404); } }
echo $form->field($model, 'short')->textArea(['rows' => 4]); ?> <?php echo $form->field($model, 'full')->textArea(); ?> <?php echo $form->field($model, 'editorTags')->widget(\sjaakp\taggable\TagEditor::className(), ['tagEditorOptions' => ['autocomplete' => ['source' => Url::toRoute(['tag/suggest'])]]])->label('Теги'); ?> <?php echo $form->field($model, 'main')->checkbox(array('label' => ''))->label('Разместить на главной'); ?> <?php echo $form->field($model, 'status')->dropDownList(\app\modules\post\models\Post::getStatusArray()); ?> <div class="alert alert-success" role="alert"> <b>SEO</b> </div> <?php echo $form->field($model, 'keywords'); ?> <?php echo $form->field($model, 'description')->textArea(['rows' => 4]); ?> <?php echo Html::submitButton($model->isNewRecord ? 'Добавить' : 'Сохранить', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
/** * 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. */ public function findModel($id) { if (($model = Post::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function getPosts() { return $this->hasMany(Post::className(), ['category_id' => 'id']); }
public function getPosts() { return $this->hasMany(Post::className(), ['id' => 'model_id'])->viaTable('tag_id', ['tag_id' => 'id']); }
/** * Deletes an existing Post model. * If deletion is successful, the browser will be redirected to the 'index' page. * * @param integer $id * @return mixed * * @throws NotFoundHttpException */ public function actionDelete($id) { $postModel = new Post(); $postModel->findModel($id)->delete(); return $this->redirect('admin'); }
/** * Deletes an existing Category model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $currentNode = $this->findModel($id); if (!$currentNode) { throw new NotFoundHttpException(Module::t('general', 'The category not found.')); } $node = Category::find()->select('id')->where(['and', ['>=', 'left', $currentNode->left], ['<=', 'right', $currentNode->right]])->asArray()->all(); try { $currentNode->deleteNode(); // if($delcategory) { Post::deleteAll(['IN', 'category_id', ArrayHelper::getColumn($node, 'id')]); \Yii::$app->session->setFlash('success', Module::t('general', 'Delete category successfull')); // } } catch (Exception $e) { \Yii::$app->session->setFlash('error', $e->getMessage()); } return $this->redirect(['index']); }