Advertisement
anggagewor

Untitled

Feb 1st, 2022
1,821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.29 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\models;
  4.  
  5. use yii\base\Model;
  6. use yii\data\ActiveDataProvider;
  7. use app\models\Clients;
  8. use yii\data\ArrayDataProvider;
  9.  
  10. /**
  11.  * ClientSearch represents the model behind the search form of `app\models\Clients`.
  12.  */
  13. class ClientSearch extends Clients
  14. {
  15.     /**
  16.      * {@inheritdoc}
  17.      */
  18.     public function rules()
  19.     {
  20.         return [
  21.             [['id'], 'integer'],
  22.             [['name', 'email', 'address', 'profession', 'created_date', 'updated_date'], '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.      */
  41.     public function search($params)
  42.     {
  43.         $query = Clients::find();
  44.         // add conditions that should always apply here
  45.  
  46.         $dataProvider = new ActiveDataProvider([
  47.             'query' => $query
  48.         ]);
  49.  
  50.         $dataProvider->setPagination(false);
  51.         $this->load($params);
  52.  
  53.         if (!$this->validate()) {
  54.             // uncomment the following line if you do not want to return any records when validation fails
  55.             // $query->where('0=1');
  56.             return $dataProvider;
  57.         }
  58.  
  59.         // grid filtering conditions
  60.         $query->andFilterWhere([
  61.             'id' => $this->id,
  62.             'created_date' => $this->created_date,
  63.             'updated_date' => $this->updated_date,
  64.         ]);
  65.  
  66.         $query->andFilterWhere(['like', 'name', $this->name]);
  67.         $models = [];
  68.         foreach ($dataProvider->models as $model)
  69.         {
  70.             if( $model->getTransactions()->count() > 0){
  71.                 $models[]=$model;
  72.             }
  73.  
  74.         }
  75.         $dataProvider->setModels($models);
  76.         $dataProvider->setTotalCount(count($models));
  77.         $dataProvider->setPagination([
  78.             'totalCount' => count($models),
  79.             'pageSizeLimit' => 20
  80.         ]);
  81.         return new ArrayDataProvider([
  82.             'allModels' => $dataProvider->models,
  83.             'sort' => [],
  84.             'pagination' => [
  85.                 'pageSize' => 20,
  86.             ],
  87.         ]);
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement