FilesSearch.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use yii\data\ActiveDataProvider;
  6. use kartik\daterange\DateRangeBehavior;
  7. /**
  8. * FilesSearch represents the model behind the search form about `app\models\Files`.
  9. */
  10. class FilesSearch extends Files
  11. {
  12. public $createTimeRange;
  13. public $createTimeStart;
  14. public $createTimeEnd;
  15. public function behaviors()
  16. {
  17. return [
  18. [
  19. 'class' => DateRangeBehavior::className(),
  20. 'attribute' => 'createTimeRange',
  21. 'dateStartAttribute' => 'createTimeStart',
  22. 'dateEndAttribute' => 'createTimeEnd',
  23. ]
  24. ];
  25. }
  26. /**
  27. * @inheritdoc
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['id', 'user_id', 'auction_id', 'lot_id'], 'integer'],
  33. [['path', 'name'], 'safe'],
  34. [['createTimeRange'], 'match', 'pattern' => '/^.+\s\-\s.+$/'],
  35. ];
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function scenarios()
  41. {
  42. // bypass scenarios() implementation in the parent class
  43. return Model::scenarios();
  44. }
  45. /**
  46. * Creates data provider instance with search query applied
  47. *
  48. * @param array $params
  49. *
  50. * @return ActiveDataProvider
  51. */
  52. public function search($params)
  53. {
  54. $query = Files::find();
  55. // add conditions that should always apply here
  56. $dataProvider = new ActiveDataProvider([
  57. 'query' => $query,
  58. ]);
  59. $this->load($params);
  60. if (!$this->validate()) {
  61. // uncomment the following line if you do not want to return any records when validation fails
  62. // $query->where('0=1');
  63. return $dataProvider;
  64. }
  65. // grid filtering conditions
  66. $query->andFilterWhere([
  67. 'id' => $this->id,
  68. 'user_id' => $this->user_id,
  69. 'auction_id' => $this->auction_id,
  70. 'lot_id' => $this->lot_id,
  71. ]);
  72. $query->andFilterWhere(['like', 'path', $this->path])
  73. ->andFilterWhere(['like', 'name', $this->name]);
  74. $query->andFilterWhere(['>=', 'createdAt', $this->createTimeStart])
  75. ->andFilterWhere(['<', 'createdAt', $this->createTimeEnd]);
  76. return $dataProvider;
  77. }
  78. }