OptionsSearch.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2020-02-23 10:24
  7. */
  8. namespace backend\models\search;
  9. use backend\models\form\AdForm;
  10. use common\libs\Constants;
  11. use common\models\Options;
  12. use yii\data\ActiveDataProvider;
  13. /**
  14. * Class OptionsSearch
  15. * @package backend\models\search
  16. *
  17. * @property integer $id
  18. * @property integer $type
  19. * @property string $name
  20. * @property string $value
  21. * @property integer $input_type
  22. * @property string $tips
  23. * @property integer $autoload
  24. * @property integer $sort
  25. */
  26. class OptionsSearch extends \yii\base\Model implements SearchInterface
  27. {
  28. public $id = null;
  29. public $type = null;
  30. public $name = null;
  31. public $value = null;
  32. public $input_type = null;
  33. public $tips = null;
  34. public $autoload = null;
  35. public $sort = null;
  36. public function rules()
  37. {
  38. return [
  39. [['id', 'sort'], 'integer'],
  40. [['type'], 'in', 'range'=>[Options::TYPE_SYSTEM, Options::TYPE_CUSTOM, Options::TYPE_BANNER, Options::TYPE_AD]],
  41. [['name', 'value', 'tips'], 'safe'],
  42. [['input_type'], 'in', 'range' => Constants::getInputTypeItems()],
  43. [['created_at', 'updated_at'], 'safe'],
  44. ];
  45. }
  46. public function search(array $params = [], array $options = [])
  47. {
  48. switch ($this->type){
  49. case Options::TYPE_AD:
  50. $query = AdForm::find()->andWhere(['type' => $this->type]);
  51. break;
  52. default:
  53. $query = Options::find()->andFilterWhere(['type' => $this->type]);
  54. }
  55. $query = $query->orderBy(['id'=>SORT_DESC]);
  56. $dataProvider = new ActiveDataProvider([
  57. 'query' => $query,
  58. ]);
  59. if (! $this->load($params)) {
  60. return $dataProvider;
  61. }
  62. $query->andFilterWhere(['id' => $this->id])
  63. ->andFilterWhere(['like', 'name', $this->name])
  64. ->andFilterWhere(['value' => $this->value])
  65. ->andFilterWhere(['input_type' => $this->input_type])
  66. ->andFilterWhere(['like', 'tips', $this->tips])
  67. ->andFilterWhere(['autoload' => $this->autoload])
  68. ->andFilterWhere(['sort' => $this->sort]);
  69. return $dataProvider;
  70. }
  71. }