I’m not sure if the title of this topic is clear enough, so let me explain myself a bit.
I have a product table for drinks. Each drink has a subType, and each subType as a type.
The types are wine, rhum, vodka and so on.
The subTypes are dependent categories. For instance:
Wine subTypes are red wine, white wine, pink wine.
Rhum subTypes are white rhum, aged rhum, golden rhum and so on.
So, in order to design a concise database and removing extra references, my product model contains only the subType attribute, because I can have access to its type by calling $model->subType->type.
Now, the tricky part comes when trying to use this indirectly related type in the GridView widget. I managed to display the type, but I can’t figue out how to filter and sort by it.
Its been a while since I’ve used a GridView component.
The reason why GridView is not allowing to sort relational data by default is that Yii usually fetches related data in seperate queries for increased performance. However, when you want to sort related data you have to tell Yii that you want to perform an INNER JOIN (or LEFT JOIN if you prefer) on the related data: