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

use yii\helpers\StringHelper;

Qiang Xue committed
5
/**
Qiang Xue committed
6 7
 * This is the template for generating a CRUD controller class file.
 *
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 28

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

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

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

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

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

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

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

70 71
        return $dataProvider;
    }
Qiang Xue committed
72

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

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