UnitSearch.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use yii\data\ActiveDataProvider;
  6. use app\models\Units;
  7. /**
  8. * UnitSearch represents the model behind the search form about `app\models\Units`.
  9. */
  10. class UnitSearch extends Units
  11. {
  12. /**
  13. * @inheritdoc
  14. */
  15. public function rules()
  16. {
  17. return [
  18. [['id'], 'integer'],
  19. [['name'], 'safe'],
  20. ];
  21. }
  22. /**
  23. * @inheritdoc
  24. */
  25. public function scenarios()
  26. {
  27. // bypass scenarios() implementation in the parent class
  28. return Model::scenarios();
  29. }
  30. /**
  31. * Creates data provider instance with search query applied
  32. *
  33. * @param array $params
  34. *
  35. * @return ActiveDataProvider
  36. */
  37. public function search($params)
  38. {
  39. $query = Units::find();
  40. // add conditions that should always apply here
  41. $dataProvider = new ActiveDataProvider([
  42. 'query' => $query,
  43. ]);
  44. $this->load($params);
  45. if (!$this->validate()) {
  46. // uncomment the following line if you do not want to return any records when validation fails
  47. // $query->where('0=1');
  48. return $dataProvider;
  49. }
  50. // grid filtering conditions
  51. $query->andFilterWhere([
  52. 'id' => $this->id,
  53. ]);
  54. $query->andFilterWhere(['like', 'name', $this->name]);
  55. return $dataProvider;
  56. }
  57. }