Advertisement
nazmiwafiy

SearchContact.php

Dec 6th, 2018
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1. <?php
  2.  
  3. namespace backend\modules\user\models;
  4.  
  5. use Yii;
  6. use yii\base\Model;
  7. use yii\data\ActiveDataProvider;
  8. use backend\modules\user\models\Contact;
  9.  
  10. /**
  11.  * SearchContact represents the model behind the search form about `frontend\models\Contact`.
  12.  */
  13. class SearchContact extends Contact
  14. {
  15.     /**
  16.      * @inheritdoc
  17.      */
  18.     public function rules()
  19.     {
  20.         return [
  21.             [['id', 'created_at', 'updated_at'], 'integer'],
  22.             [['name', 'email'], 'safe'],
  23.         ];
  24.     }
  25.  
  26.     /**
  27.      * @inheritdoc
  28.      */
  29.     public function scenarios()
  30.     {
  31.         // bypass scenarios() implementation in the parent class
  32.         return Model::scenarios();
  33.     }
  34.  
  35.     /**
  36.      * Creates data provider instance with search query applied
  37.      *
  38.      * @param array $params
  39.      *
  40.      * @return ActiveDataProvider
  41.      */
  42.     public function search($params)
  43.     {
  44.         $query = Contact::find();
  45.  
  46.         $dataProvider = new ActiveDataProvider([
  47.             'query' => $query,
  48.         ]);
  49.  
  50.         $this->load($params);
  51.  
  52.         if (!$this->validate()) {
  53.             // uncomment the following line if you do not want to return any records when validation fails
  54.             // $query->where('0=1');
  55.             return $dataProvider;
  56.         }
  57.  
  58.         $query->andFilterWhere(['like', 'name', $this->name])
  59.             ->andFilterWhere(['like', 'email', $this->email]);
  60.  
  61.         return $dataProvider;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement