search.php 1.76 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;
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
    public function rules()
    {
        return [
41
            <?= implode(",\n            ", $rules) ?>,
42 43
        ];
    }
Qiang Xue committed
44

45 46 47 48 49 50
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

51 52
    public function search($params)
    {
53
        $query = <?= isset($modelAlias) ? $modelAlias : $modelClass ?>::find();
54

55 56 57
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
Qiang Xue committed
58

59 60 61
        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }
Qiang Xue committed
62

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

65 66
        return $dataProvider;
    }
Qiang Xue committed
67
}