In my users table I have an "enabled" field - this can be set to either 1 or 0.
I want to be able to set up in my model a way of transforming this value to a more friendly value i.e. 1="Yes", 0="No".
I can of course do this manually in each view via IF statements but ideally I want this process to be automated in the model so that it’s available anywhere that I call $model->enabled - is this possible?
Thanks, that’s exactly what my colleague has suggested, but I was hoping there was a way of doing this automatically, i.e. without having to call any function?
If I understand right you have a field "enabled" that is 1 or 0, but in the application you would like it to be "yes" or "no"…
You could use a custom behavior to change the value of enabled, but I’m not sure if that is goot programing practice…
Something like this would work:
myEnable.php - put it for example in protected/components
class myEnable extends CActiveRecordBehavior
{
/**
* Convert from Yes/No to 1/0
*/
public function beforeSave($event)
{
$this->Owner->enabled = $this->Owner->enabled == 'Yes' ? '1' : '0';
return parent::beforeSave($event);
}
/**
* Convert from 1/0 to Yes/No
*/
public function afterFind($event)
{
$this->Owner->enabled = $this->Owner->enabled ? 'Yes' : 'No';
return parent::afterFind($event);
}
}
now in the model you attach this behavior like:
public function behaviors(){
return array(
'myEnable'=>array(
'class'=>'application.components.myEnable',
),
);
}
Note that with this in place the enabled property now is Yes or No, not anymore 1 or 0… so if you make create/updates rules, you have to check not for integer 1/0 but for text ‘Yes’/‘No’.
Sure it works… the behavior variant is just to make it a re-usable component…
NOTE: If you are going to save some data for this model than you should add the beforeSave() method that returns the value to what the database expects.
mdomba’s approach is right on the ball for your situation but not the best thing to do. I take it in your code you’ll want to use the 1/0 representation instead of ‘Yes’/‘No’. eg:
if($model->enabled == 1)
instead of
if($model->enabled == 'Yes')
… Otherwise i don’t see any good reason to store 1/0 in the field when you always use it as ‘Yes’/‘No’.
What I usually do is define class constants to represent states e.g. for your case i’d do
const ENABLED_NO = 0;
const ENABLED_YES = 1;
so a comparison
if($model->enabled == User::ENABLED_NO)
is more meaningful to a reader than
if($model->enabled == 0)
It’s fine to use 1/0 if you’ll be the only one reading the source code and/or if it’s properly documented.
Per the API documentation, you can pass expressions into CGridView (and I assume CDetailView).
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'title', // display the 'title' attribute
'category.name', // display the 'name' attribute of the 'category' relation
'content:html', // display the 'content' attribute as purified HTML
array( // display 'create_time' using an expression
'name'=>'create_time',
'value'=>'date("M j, Y", $data->create_time)',
),
array( [b]// display 'author.username' using an expression[/b]
'name'=>'authorName',
'value'=>'$data->author->username',
),
array( // display a column with "view", "update" and "delete" buttons
'class'=>'CButtonColumn',
),
),
));