Skip to content

Commit

Permalink
feat: images search
Browse files Browse the repository at this point in the history
  • Loading branch information
ufaboy committed Sep 24, 2023
1 parent e80af07 commit 245b580
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion common/models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use yii\helpers\ArrayHelper;

/**
* This is the model class for table "media".
* This is the model class for table "image".
*
* @property int $id
* @property string|null $file_name
Expand Down
84 changes: 83 additions & 1 deletion modules/api/models/ImageSearch.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,89 @@
<?php

namespace app\modules\api\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\api\models\Image;
use Yii;
use yii\helpers\VarDumper;

class ImageSearch extends \app\common\models\ImageSearch {
class ImageSearch extends Image {
public string $bookName = '';

public function attributes(): array {
return array_merge(parent::attributes(), ['book.name']);
}

public function fields(): array {
return [
'id',
'file_name',
'path',
'book_id',
];
}
public function extraFields() {
return [
'book',
];
}
/**
* {@inheritdoc}
*/
public function rules(): array {
return [
[['id', 'book_id'], 'integer'],
[['file_name', 'book_id', 'path', 'bookName', 'book.name'], 'safe'],
];
}

/**
* {@inheritdoc}
*/
public function scenarios(): array {
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}

/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params): ActiveDataProvider {
$query = self::find();

$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$query->joinWith('book');
$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,
'book_id' => $this->book_id,
]);
$query->andFilterWhere(
['ilike', 'book.name', $this->bookName]
);
$query->andFilterWhere(['ilike', 'file_name', $this->file_name])
->andFilterWhere(['ilike', 'path', $this->path])
->andFilterWhere(['ilike', 'book.name', $this->getAttribute('book.name')]);

$query->groupBy('image.id');
$dataProvider->sort->attributes['book.name'] = [
'asc' => ['book.name' => SORT_ASC],
'desc' => ['book.name' => SORT_DESC],
];
return $dataProvider;
}
}

0 comments on commit 245b580

Please sign in to comment.