Petruza
Petruza

Reputation: 12256

Can't sort related columns of CGridView widget, Yii 1.1

I'm using this DataProvider to feed a CGridView widget, but the columns still don't show as clickable and of course don't allow sorting.
What's wrong here?

Controller:

$dataProvider = new CActiveDataProvider( 'SubscriberAuthor', [
    'criteria' => [
        'with' => [
            'subscriber' => [ 'select' => 'name, email' ]
        ],
        'condition' => "author_id = {$author_id} And verified = 1"
    ],
    'sort' => [
        'attributes' => [
            'name'=> [
                'asc'=>'subscriber.name Asc',
                'desc'=>'subscriber.name Desc',
            ],
            'email'=> [
                'asc'=>'subscriber.email Asc',
                'desc'=>'subscriber.email Desc'
            ]
        ]
    ],
    'countCriteria' => [ 'condition' => "author_id = {$author_id} And verified = 1" ]
]);

View:

$this->widget( 'zii.widgets.grid.CGridView', [
    'dataProvider' => $dataProvider,
    'enableSorting' => true,
    'columns' => [ 'subscriber.name', 'subscriber.email' ]
]);

Upvotes: 0

Views: 115

Answers (2)

Petruza
Petruza

Reputation: 12256

Of course, the problem was I was not using the fully qualified names of the columns:

'sort' => [
    'attributes' => [
        'subscriber.name'=> [
            'asc'=>'subscriber.name Asc',
            'desc'=>'subscriber.name Desc',
        ],
        'subscriber.email'=> [
            'asc'=>'subscriber.email Asc',
            'desc'=>'subscriber.email Desc'
        ]
    ]
]

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133360

be sure that in the column name you are using the table name and not the relation or the model name

        'attributes' => [
        'name'=> [
            'asc'=>'subscriber_table.name Asc',
            'desc'=>'subscriber_table.name Desc',
        ],
        'email'=> [
            'asc'=>'subscriber_table.email Asc',
            'desc'=>'subscriber_table.email Desc'
        ]

Upvotes: 1

Related Questions