public function fields() { $fields = parent::fields(); // remove fields that contain sensitive information //unset($fields['auth_key'], $fields['password_hash'], $fields['password_reset_token']); $fields[] = 'links'; return $fields; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = BlogPost::find(); $query->orderBy(['created_at' => SORT_DESC]); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'catalog_id' => $this->catalog_id, 'click' => $this->click, 'user_id' => $this->user_id, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'tags', $this->tags])->andFilterWhere(['like', 'surname', $this->surname]); return $dataProvider; }
$this->params['breadcrumbs'][] = $this->title; ?> <div class="blog-post-index"> <?php // echo $this->render('_search', ['model' => $searchModel]); ?> <p> <?php echo Html::a(Module::t('blog', 'Create ') . Module::t('blog', 'Blog Post'), ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\CheckboxColumn'], ['attribute' => 'catalog_id', 'value' => function ($model) { return $model->catalog->title; }, 'filter' => Html::activeDropDownList($searchModel, 'catalog_id', \funson86\blog\models\BlogPost::getArrayCatalog(), ['class' => 'form-control', 'prompt' => Module::t('blog', 'Please Filter')])], 'title', 'commentsCount', ['attribute' => 'status', 'format' => 'html', 'value' => function ($model) { if ($model->status === Status::STATUS_ACTIVE) { $class = 'label-success'; } elseif ($model->status === Status::STATUS_INACTIVE) { $class = 'label-warning'; } else { $class = 'label-danger'; } return '<span class="label ' . $class . '">' . $model->getStatus()->label . '</span>'; }, 'filter' => Html::activeDropDownList($searchModel, 'status', Status::labels(), ['class' => 'form-control', 'prompt' => Module::t('blog', 'PROMPT_STATUS')])], 'created_at:date', ['class' => 'yii\\grid\\ActionColumn']]]); ?> </div>
public function actionView() { if (Yii::$app->request->get('id') && Yii::$app->request->get('id') > 0) { $post = BlogPost::findOne(Yii::$app->request->get('id')); $post->updateCounters(['click' => 1]); $comments = BlogComment::find()->where(['post_id' => $post->id, 'status' => Status::STATUS_ACTIVE])->orderBy(['created_at' => SORT_ASC])->all(); $comment = $this->newComment($post); } else { $this->redirect(['blog']); } //var_dump($post->comments); return $this->render('view', ['post' => $post, 'comments' => $comments, 'comment' => $comment]); }
public function run() { $posts = BlogPost::find()->where(['status' => Status::STATUS_ACTIVE])->orderBy(['click' => SORT_DESC])->limit($this->maxPosts)->all(); return $this->render('recentPosts', ['title' => $this->title, 'posts' => $posts]); }
/** * @return \yii\db\ActiveQuery */ public function getBlogPost() { return $this->hasOne(BlogPost::className(), ['id' => 'post_id']); }
/** * Finds the BlogPost model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return BlogPost the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = BlogPost::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
use yii\widgets\ActiveForm; use funson86\blog\models\BlogPost; use yii\helpers\ArrayHelper; /* @var $this yii\web\View */ /* @var $model backend\modules\blog\models\BlogComment */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="blog-comment-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'post_id')->dropDownList(ArrayHelper::map(BlogPost::find()->all(), 'id', 'title')); ?> <?php echo $form->field($model, 'content')->textarea(['rows' => 6]); ?> <?php echo $form->field($model, 'author')->textInput(['maxlength' => 128]); ?> <?php echo $form->field($model, 'email')->textInput(['maxlength' => 128]); ?> <?php
public function getPostsCount() { return $this->count(BlogPost::className(), ['catalog_id' => 'id']); }