search.php 2.43 KB
Newer Older
Qiang Xue committed
1
<?php
Qiang Xue committed
2 3 4

use yii\helpers\StringHelper;

Qiang Xue committed
5
/**
6
 * This is the template for generating CRUD search class of the specified model.
Qiang Xue committed
7
 *
Alexander Makarov committed
8
 * @var yii\web\View $this
Qiang Xue committed
9
 * @var yii\gii\generators\crud\Generator $generator
Qiang Xue committed
10
 */
Qiang Xue committed
11 12 13

$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
14
if ($modelClass === $searchModelClass) {
15
    $modelAlias = $modelClass . 'Model';
16
}
Qiang Xue committed
17 18 19 20 21 22 23 24
$rules = $generator->generateSearchRules();
$labels = $generator->generateSearchLabels();
$searchAttributes = $generator->getSearchAttributes();
$searchConditions = $generator->generateSearchConditions();

echo "<?php\n";
?>

Alexander Makarov committed
25
namespace <?= StringHelper::dirname(ltrim($generator->searchModelClass, '\\')) ?>;
Qiang Xue committed
26

27
use Yii;
Qiang Xue committed
28 29
use yii\base\Model;
use yii\data\ActiveDataProvider;
30
use <?= ltrim($generator->modelClass, '\\') . (isset($modelAlias) ? " as $modelAlias" : "") ?>;
Qiang Xue committed
31 32

/**
33
 * <?= $searchModelClass ?> represents the model behind the search form about `<?= $generator->modelClass ?>`.
Qiang Xue committed
34
 */
Alexander Makarov committed
35
class <?= $searchModelClass ?> extends Model
Qiang Xue committed
36
{
37
    public $<?= implode(";\n    public $", $searchAttributes) ?>;
Qiang Xue committed
38

39 40 41
    public function rules()
    {
        return [
42
            <?= implode(",\n            ", $rules) ?>,
43 44
        ];
    }
Qiang Xue committed
45

46 47 48 49 50 51
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
Qiang Xue committed
52
<?php foreach ($labels as $name => $label): ?>
Luciano Baraglia committed
53
            <?= "'$name' => " . $generator->generateString($label) . ",\n" ?>
Qiang Xue committed
54
<?php endforeach; ?>
55 56
        ];
    }
Qiang Xue committed
57

58 59 60 61 62 63
    public function search($params)
    {
        $query = <?= isset($modelAlias) ? $modelAlias : $modelClass ?>::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
Qiang Xue committed
64

65 66 67
        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }
Qiang Xue committed
68

69
        <?= implode("\n        ", $searchConditions) ?>
Qiang Xue committed
70

71 72
        return $dataProvider;
    }
Qiang Xue committed
73

74 75 76 77 78 79 80
    protected function addCondition($query, $attribute, $partialMatch = false)
    {
        if (($pos = strrpos($attribute, '.')) !== false) {
            $modelAttribute = substr($attribute, $pos + 1);
        } else {
            $modelAttribute = $attribute;
        }
81

82 83 84 85 86 87 88 89 90 91
        $value = $this->$modelAttribute;
        if (trim($value) === '') {
            return;
        }
        if ($partialMatch) {
            $query->andWhere(['like', $attribute, $value]);
        } else {
            $query->andWhere([$attribute => $value]);
        }
    }
Qiang Xue committed
92
}