I checked the guide and the other posts on gridview and they didn’t address the specific issue I’m trying to address, so I’m posting this as a new topic.
I’m storing the value of gender as an integer on a profile model, 0 for female, 1 for male, so when I display the index view of profile, I get a list of profiles and in the gender column, I see 0’s and 1’s. This is expected behavior, but I would rather show the text definition of gender, which is either ‘male’ or ‘female’.
Ideally I would like to do something like this on the grid widget:
‘columns’ => [
'id',
['label'=>'Gender', 'value'=>MyClass::myMethodToLookUpDisplayText($modelwewant, $attribute, $?)]
];
This is a static class I built to manage simple dropdown lists. $modelwewant and $attribute work perfectly. They tell the method which model we are working with and which attribute the options belong to. $? would be the specific value that let’s the method return the display text for that value. In the case of this example, where I’m trying to return gender on the profile model, it would look like this:
‘columns’ => [
'id',
['label'=>'Gender', 'value'=>MyClass::myMethodToLookUpDisplayText('profile', 'gender', $?)]
];
I’m stuck at how to pass in the value of gender at $?. It needs to be the integer value of the attribute, which is how it is stored in the db. Then MyClass will return the display text. Of course that doesn’t mean this example will work exactly. I tried passing in $searchModel->gender but that generated an error, where it said it couldn’t find Female as an attribute of profile. A string conversion problem? In this case, I think the grid expects ‘value’ to be an integer.
Also, although I would like to solve this and try it this way, I recognize this might not be the best solution. One problem I can see is that this creates a query for every row in the grid, which for large data sets would be inefficient use of the server or even downright crazy.
I’m trying to create a centralized solution with a ui for managing dropdown options, allowing option in range rules, and display text. I’ve been really happy with the results so far, but got stuck here. If I can get it working to my satisfaction I will share it in a wiki. Before I do that, I need to make sure that I’m on the right path with this idea.
So the second part of this question is to ask about best practices. Is there a way to do this with eager loading? I know I can use constants on the model to represent the display text of the values, but I’m trying to avoid this, it seems like really messy code to me to stick a bunch of constants on the models for this purpose. I could also create a lookup table to hold the display text and create relations between models, but this seems excessive to build a model to house two strings of text, and to do that everytime I want to display text instead of an integer. I’m trying to increase efficiency, optimize workflow, and keep the code clean. Any thoughts or help would be greatly appreciated, thank you.