SORT problem with "3d part STAT relations"

Hi everybody!

Faced with the SORT problem with "3d part STAT relations"! Solved it, but decision is disgusting! Maybe someone will offer better solution.

So, I have 3 tables:


item:

id

user_id

is_active


user:

user_id


shop:

id

user_id


models and relations:

User relations:


return [

    'CountActiveItems' => [

        self::STAT,

        'Item',

        'user_id',

        'condition' => 'is_active = 0',

    ],

    'CountSoldItems' => [

        self::STAT,

        'Item',

        'user_id',

        'condition' => 'is_active = 1',

    ],

];

Shop relations:


return [

    'User' => [

        self::BELONGS_TO,

        'User',

        'user_id',

    ],

];


My Shop griview:


$this->widget('zii.widgets.grid.CGridView', [

	'dataProvider' => $model->search(),

	'filter' => $model,

	'columns' => [

        ...

		[

			'name' => 'countActiveItems',

			'header' => CHtml::encode($model->getAttributeLabel('countActiveItems')),

			'value' => '$data->User instanceOf User ? $data->User->CountActiveItems : 0',

			'filter' => CHtml::activeTextField($model, 'countActiveItems'),

		],

		[

			'name' => 'countSoldItems',

			'header' => CHtml::encode($model->getAttributeLabel('countSoldItems')),

			'value' => '$data->User instanceOf User ? $data->User->CountSoldItems : 0',

			'filter' => CHtml::activeTextField($model, 'countSoldItems'),

		],

		...

And my Shop search method:


$criteria = new CDbCriteria;

$sort = new CSort;


$criteria->compare('id', $this->id);

$criteria->compare('user_id', $this->user_id);


$criteria->mergeWith(new CDbCriteria([

    'select' => '*, (select count(*) from item where user_id = t.user_id and is_active = 0) as CountActiveItems',

]));


$criteria->mergeWith(new CDbCriteria([

    'select' => '*, (select count(*) from item where user_id = t.user_id and is_active = 1) as CountSoldItems',

]));




$sort->attributes = [

    'countActiveItems' => [

        'asc' => 'CountActiveItems',

        'desc' => 'CountActiveItems desc',

    ],

    'countSoldItems' => [

        'asc' => 'CountSoldItems',

        'desc' => 'CountSoldItems desc',

    ],

    '*',

];


$sort->defaultOrder = ['id' => CSort::SORT_DESC,];


return new CActiveDataProvider($this, [

    'criteria' => $criteria,

    'sort' => $sort,

]);


I’ve tried do something like that:


$criteria->with = ['User.CountActiveItems', 'User.CountSoldItems',];

$criteria->together = true;

and




$sort->attributes = [

    'countActiveItems' => [

        'asc' => 'User.CountActiveItems',

        'desc' => 'User.CountActiveItems desc',

    ],

    'countSoldItems' => [

        'asc' => 'User.CountSoldItems',

        'desc' => 'User.CountSoldItems desc',

    ],

    '*',

];



but got mistake that ‘column User.CountActiveItems does not exist’