OrderSearch.php 5.01 KB
<?php
    
    namespace artbox\order\models;
    
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    
    /**
     * OrderSearch represents the model behind the search form about `artbox\order\models\Order`.
     */
    class OrderSearch extends Order
    {
        
        public $count;
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        //                        'id',
                        'user_id',
                        'label_id',
                    ],
                    'integer',
                ],
                [
                    [
                        'name',
                        'secondname',
                        'phone',
                        'email',
                    ],
                    'safe',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function scenarios()
        {
            // bypass scenarios() implementation in the parent class
            return Model::scenarios();
        }
        
        /**
         * Creates data provider instance with search query applied
         *
         * @param array $params
         *
         * @return ActiveDataProvider
         */
        public function search($params)
        {
            $query = Order::find()
                          ->joinWith('orderProducts')
                          ->with('label.lang')
                          ->select(
                              [
                                  'order.*',
                                  'count(*)',
                              ]
                          )
                          ->groupBy('order.id');
            
            // add conditions that should always apply here
            
            $dataProvider = new ActiveDataProvider(
                [
                    'query' => $query,
                    'sort'  => [
                        'attributes'   => [
                            'count',
                            'id',
                            'user_id',
                            'name',
                            'secondname',
                            'label_id',
                            'created_at',
                            'phone',
                            'email',
                        ],
                        'defaultOrder' => [
                            'created_at' => SORT_DESC,
                        ],
                    ],
                ]
            );
            
            $this->load($params);
            
            if (!$this->validate()) {
                // uncomment the following line if you do not want to return any records when validation fails
                // $query->where('0=1');
                return $dataProvider;
            }
            
            // grid filtering conditions
            $query->andFilterWhere(
                [
                    //                    'id'          => $this->id,
                    'user_id'     => $this->user_id,
                    'label_id'    => $this->label_id,
                    'delivery_id' => $this->delivery_id,
                    'payment_id'  => $this->payment_id,
                    'created_at'  => $this->created_at,
                    'updated_at'  => $this->updated_at,
                    'deleted_at'  => $this->deleted_at,
                    'count'       => $this->count,
                ]
            );
            
            $query->andFilterWhere(
                [
                    'ilike',
                    'name',
                    $this->name,
                ]
            )
                  ->andFilterWhere(
                      [
                          'ilike',
                          'secondname',
                          $this->secondname,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'like',
                          'phone',
                          $this->phone,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'ilike',
                          'email',
                          $this->email,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'ilike',
                          'city',
                          $this->city,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'ilike',
                          'address',
                          $this->address,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'ilike',
                          'comment',
                          $this->comment,
                      ]
                  );
            
            return $dataProvider;
        }
    }