search.php 1.94 KB
Newer Older
Qiang Xue committed
1 2
<?php
/**
3
 * This is the template for generating CRUD search class of the specified model.
Qiang Xue committed
4
 */
Qiang Xue committed
5

6 7 8 9 10 11
use yii\helpers\StringHelper;


/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */

Qiang Xue committed
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;
28
use yii\base\Model;
Qiang Xue committed
29
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
 */
35
class <?= $searchModelClass ?> extends <?= isset($modelAlias) ? $modelAlias : $modelClass ?>
Qiang Xue committed
36

37
{
38 39 40
    /**
     * @inheritdoc
     */
41 42 43
    public function rules()
    {
        return [
44
            <?= implode(",\n            ", $rules) ?>,
45 46
        ];
    }
Qiang Xue committed
47

48 49 50
    /**
     * @inheritdoc
     */
51 52 53 54 55 56
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

57 58 59 60
    /**
     * Creates data provider instance with search query applied
     * @return ActiveDataProvider
     */
61 62
    public function search($params)
    {
63
        $query = <?= isset($modelAlias) ? $modelAlias : $modelClass ?>::find();
64

65 66 67
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
Qiang Xue committed
68

69 70 71
        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }
Qiang Xue committed
72

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

75 76
        return $dataProvider;
    }
Qiang Xue committed
77
}