Здравствуйте. Реализую таблицу через Gridview и 2 нужных мне столбца не активируют сортировку, не понимаю, почему так.
Код виджета:
\yii\grid\GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'id',
'options' => ['style' => 'width: 20px;'],
],
[
'attribute' => 'count_days',
'value' => 'countDays',
'header' => 'Дни',
'enableSorting' => true
],
[
'attribute' => 'count_orders',
'header' => 'Заявок',
'value' => 'countOrders',
],
[
'attribute' => 'expire_date',
'value' => function ($model) {
/** @var \app\models\NewTours $model */
if (!$model->expire_date) {
return 'Не оплачено';
}
return date('d.m.Y', $model->expire_date);
}
],
],
])
Код модели:
<?php
namespace app\components\administrator\models;
use app\models\NewTours;
use app\models\RequestsToursGuides;
use app\models\ToursPlan;
use yii\data\ActiveDataProvider;
use yii\base\Model;
class ToursSearchAdmin extends NewTours
{
public $is_active;
public $author;
public $count_days;
public $count_orders;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'is_active', 'count_days', 'count_orders'], 'integer'],
[['title', 'author'], 'string']
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
public function search($params)
{
$subQuery = ToursPlan::find()->select('tour_id, COUNT(tour_id) as count_plan')->groupBy('tour_id');
$query = self::find();
$query->leftJoin(['count_days' => $subQuery], 'count_days.tour_id = new_tours.id');
$subQuery = RequestsToursGuides::find()->select('object_id, COUNT(object_id) as count_items')->where(['object_type' => 'tour'])->groupBy('object_id');
$query->leftJoin(['count_orders' => $subQuery], 'count_orders.object_id = new_tours.id');
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 30,
],
'sort' => [
'defaultOrder' => ['count_orders' => SORT_DESC],
'attributes' => ['id', 'expire_date',
'count_days' => [
'asc' => [
'count_plan' => SORT_ASC
],
'desc' => [
'count_plan' => SORT_DESC
]
],
'count_orders' => [
'asc' => [
'count_items' => SORT_ASC
],
'desc' => [
'count_items' => SORT_DESC
]
],
]
]
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'count_days' => $this->count_days,
'count_orders' => $this->count_orders
]);
if ($this->author) {
$query->leftJoin('user_info', 'user_info.user_id = new_tours.user_id');
$query->leftJoin('company_info', 'company_info.user_id = new_tours.user_id');
$query->andWhere('company_info.name like "%'.$this->author.'%" OR concat(user_info.name," ",user_info.last_name) like "%'.$this->author.'%"');
}
if ($this->is_active == '0') {
$query->andWhere('active = 0 or expire_date < '.time());
}
elseif ($this->is_active == '1') {
$query->andWhere('active = 1 and expire_date > '.time());
}
$query->andFilterWhere(['like','title', $this->title]);
return $dataProvider;
}
}
В итоге вижу следующую картину:
Не понимаю, почему столбцы "Дни" и "Заявок" не активны для сортировки как столбец "Срок действия". Причем, если я в урле пропишу
?sort=-count_days
сортировка применится как нужно. Подскажите, пожалуйста, что я делаю не так?
Заранее благодарен всем отозвавшимся.