select( '`user`.id, ' .'`user`.username,' .'`user`.email, ' .'`user`.status, ' .'`user`.created_at, ' .'`user`.updated_at, ' .'`user`.lastname, ' .'`user`.name, ' .'`user`.phone, ' .'`user`.address, ' .'`user`.no_mail, ' .'`user`.mail_distribution_monday, ' .'`user`.mail_distribution_tuesday, ' .'`user`.mail_distribution_wednesday, ' .'`user`.mail_distribution_thursday, ' .'`user`.mail_distribution_friday, ' .'`user`.mail_distribution_saturday, ' .'`user`.mail_distribution_sunday, ' .'`user`.id_producer, ' .'`user`.date_last_connection, ' . '(SELECT COUNT(*) FROM `order` WHERE `user`.id = `order`.id_user) AS count_orders'); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'sort' => ['attributes' => ['username','credit','orders']], 'pagination' => [ 'pageSize' => 20, ], ]); $dataProvider->sort->attributes['username'] = [ 'asc' => ['user.lastname' => SORT_ASC, 'user.name' => SORT_ASC], 'desc' => ['user.lastname' => SORT_DESC, 'user.name' => SORT_DESC], ]; $dataProvider->sort->attributes['credit'] = [ 'asc' => ['user_producer.credit' => SORT_ASC], 'desc' => ['user_producer.credit' => SORT_DESC], ]; $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->innerJoin('user_producer','user.id = user_producer.id_user AND user_producer.id_producer = :id_producer AND user_producer.active = 1', [':id_producer' => Producer::getId()]) ; if(isset($this->id_point_sale) && $this->id_point_sale) { $pointSale = PointSale::findOne(['id' => $this->id_point_sale]) ; $query->innerJoin('user_point_sale','user.id = user_point_sale.id_user AND user_point_sale.id_point_sale = :id_point_sale',[':id_point_sale' => $this->id_point_sale]) ; } if(isset($this->subscribers) && $this->subscribers) { $query->innerJoin( 'subscription', 'user.id = subscription.id_user AND subscription.id_producer = :id_producer', [':id_producer' => Producer::getId()] )->groupBy('user.id') ; } if(isset($this->inactive) && $this->inactive) { $query->having([ 'count_orders' => 0 ]) ; } $query->andFilterWhere([ 'or', ['like', 'user.lastname', $this->username], ['like', 'user.name', $this->username] ]); return $dataProvider; } }