This post details how a Lookup Model can be created to centrally maintain a set of code-to-text translations.
My question is when using CListView or CGridView to sort data in models that make use of the Lookup Model, how do you go about sorting the results based on the looked-up text representation rather than the original integer code?
The ordering is based on the data (for example in database). In Which column do you want to achieved the order? If you want to sorting with other way (after of loading) you may have to do that manually
That’s provided by the Lookup model in the link I gave
I’d seen that but couldn’t see how to make it work in this case.
The trouble as I see it is caused by the fact that the Lookup model handles code/string lookups for multiple attributes in multiple other data models. I’m sure this is a common technique - I know I’ve used it many times in the past in other environments. The trouble is when using Yii I’ve not found a way to express the relationship between the two models via the “relations” mechanism. So therefore I can’t make use of the linked article.
So maybe a better question for me to ask to ask would be how to add a relation between any old data model and the Lookup model in the OP?
I’m using something similar to the model described here in one of the Yii tutotials. The Lookup table (Dictionary in your verbiage) contains a number of code/name pairs grouped by type:
id: the primary key of the lookup item. Only used for Lookup table maintenance
name: the textual representation of the data item that is to be displayed to end users;
code: the integer representation of the data item;
type: the type of the data item;
position: the relative display order of the data item among other items of the same type.
For this to be used in a particular data model, one or more attributes in the model would have a type defined in the Lookup model. Retrieving all code/name pairs for a given type would allow dropdown lists to be populated (using the order specified by the position attribute in the Lookup table). Looking up a particular code of a specific type would enable the textual value (name) to be retrieved.
So I think the main difference (and complexity) over the scenario you presented is that in your case the code is actually the id and will be unique across the entire system. In the above Lookup model, the code is only unique for all rows that have the same type; this provides much more flexibility in the use of the code values. But my inability to specify this relationship in the relations array is proving a problem.
Because the primary key in the Lookup model does not form part of the relationship - only the code & type fields do.
I’ve actually got a solution now, the key to which was derived from this thread that shows that the ForeignKey entry is not actually needed when specifying relations - that was news to me.
This does require eager loading of the Lookup model, though. So I modified the search() method in the model to include a with criteria and added a sort specification to the CActiveDataProvider constructor: