/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Category::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, 'status' => $this->status]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
<?php use dosamigos\ckeditor\CKEditor; use obuhovski\blog\models\Category; use yii\bootstrap\Modal; use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $this yii\web\View */ /* @var $model obuhovski\blog\models\Post */ /* @var $form yii\widgets\ActiveForm */ $categoriesList = Category::find()->select('name, id')->where(['status' => 1])->indexBy('id')->asArray()->column(); ?> <div class="post-form"> <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> <img class="img-responsive" src="<?php echo $model->getThumbUploadUrl('image'); ?> " alt="" /> <?php echo $form->field($model, 'image')->fileInput(); ?> <?php echo $form->field($model, 'title')->textInput(['maxlength' => true]); ?>
public function init() { parent::init(); $categories = Category::find()->where(['status' => 1])->with('posts')->all(); echo $this->render('categories', ['categories' => $categories]); }
/** * @return \yii\db\ActiveQuery */ public function getCategories() { return $this->hasMany(Category::className(), ['id' => 'category_id'])->viaTable('posts_categories', ['post_id' => 'id']); }
/** * Finds the Category model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Category the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Category::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }