You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2KB

  1. <?php
  2. /**
  3. * This is the template for generating CRUD search class of the specified model.
  4. */
  5. use yii\helpers\StringHelper;
  6. /* @var $this yii\web\View */
  7. /* @var $generator yii\gii\generators\crud\Generator */
  8. $modelClass = StringHelper::basename($generator->modelClass);
  9. $searchModelClass = StringHelper::basename($generator->searchModelClass);
  10. if ($modelClass === $searchModelClass) {
  11. $modelAlias = $modelClass . 'Model';
  12. }
  13. $rules = $generator->generateSearchRules();
  14. $labels = $generator->generateSearchLabels();
  15. $searchAttributes = $generator->getSearchAttributes();
  16. $searchConditions = $generator->generateSearchConditions();
  17. echo "<?php\n";
  18. ?>
  19. namespace <?= StringHelper::dirname(ltrim($generator->searchModelClass, '\\')) ?>;
  20. use Yii;
  21. use yii\base\Model;
  22. use yii\data\ActiveDataProvider;
  23. use <?= ltrim($generator->modelClass, '\\') . (isset($modelAlias) ? " as $modelAlias" : "") ?>;
  24. /**
  25. * <?= $searchModelClass ?> represents the model behind the search form about `<?= $generator->modelClass ?>`.
  26. */
  27. class <?= $searchModelClass ?> extends <?= isset($modelAlias) ? $modelAlias : $modelClass ?>
  28. {
  29. /**
  30. * @inheritdoc
  31. */
  32. public function rules()
  33. {
  34. return [
  35. <?= implode(",\n ", $rules) ?>,
  36. ];
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function scenarios()
  42. {
  43. // bypass scenarios() implementation in the parent class
  44. return Model::scenarios();
  45. }
  46. /**
  47. * Creates data provider instance with search query applied
  48. *
  49. * @param array $params
  50. *
  51. * @return ActiveDataProvider
  52. */
  53. public function search($params)
  54. {
  55. $query = <?= isset($modelAlias) ? $modelAlias : $modelClass ?>::find();
  56. // add conditions that should always apply here
  57. $dataProvider = new ActiveDataProvider([
  58. 'query' => $query,
  59. ]);
  60. $this->load($params);
  61. if (!$this->validate()) {
  62. // uncomment the following line if you do not want to return any records when validation fails
  63. // $query->where('0=1');
  64. return $dataProvider;
  65. }
  66. // grid filtering conditions
  67. <?= implode("\n ", $searchConditions) ?>
  68. return $dataProvider;
  69. }
  70. }