/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = CategoryImage::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, 'id_category' => $this->id_category]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
/** * Finds the CategoryImage model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return CategoryImage the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = CategoryImage::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
echo Html::encode($this->title); ?> </h1> <?php // echo $this->render('_search', ['model' => $searchModel]); ?> <p> <?php echo Html::a('Create Category', ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], ['attribute' => 'image', 'format' => 'html', 'value' => function ($model) { $image = CategoryImage::find()->where(['id_category' => $model->id])->min('name'); $file = Yii::getAlias('@category/' . $model->id . '/' . $image); if (is_file($file)) { $thumb = Yii::getAlias('@category/' . $model->id . '/thumb' . $image); if (file_exists($thumb)) { } else { Image::thumbnail($file, 120, 120)->save(Yii::getAlias('@category/' . $model->id . '/thumb' . $image), ['quality' => 80]); } return Html::img(Yii::getAlias('@category/' . $model->id . '/thumb' . $image)); } else { return Html::img(Yii::getAlias('http://ixioo.com/upload/images/no-photo.jpg')); } }], 'id', 'name', 'url_official:url', ['class' => 'yii\\grid\\ActionColumn']]]); ?> </div>
public function beforeDelete() { if (parent::beforeDelete()) { CategoryImage::deleteAll(['id_category' => $this->id]); BaseFileHelper::removeDirectory(Yii::getAlias('@category/') . $this->id); return true; } else { return false; } }