cDetailview Display alias name yii

125 views Asked by At

My view code

<?php $this->widget('zii.widgets.CDetailView', array(
                    'data'=>$model,
                    'attributes'=>array(
                        'id',
                        'eventstype',
                        'visibility',
                        'enable',
                    ),
                )); ?>

controller code

public function actionView($id)
    {
        $model = ManageEventsType::model()->findByAttributes(array("id" => $id));
                if($model){
                $this->render("view", array(
                    "model" => $model
                ));
                }
    }

in my view page the records display like following

Id          3
Eventstype  Holiday
Visibility  2
Enable      0

i want to display visibility as enable or disable . 1- enable , 2- Disable , any idea

2

There are 2 answers

2
Developerium On BEST ANSWER
$text = $model->visibility == 1 ? 'enable' : 'disabled';

$this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
        'id',
        'eventstype',
    array(
       'name' => 'visibility',
       'value' => $text,
    ),

    ),
)); ?>
0
crafter On

The 'elegant' way to do this is to change your ActiveRecord model.

class ManageEventsType extends CActiveRecord
{

   /* Give it a name that is meaningful to you */
   public $visibility_text;

   ...

}

This will extend your model by creating an additional attribute.

Inside your model, you then add (and overwrite) the afterFind() function.

class ManageEventsType extends CActiveRecord    
{
    public $visibility_text;
    protected function afterFind ()
    {
        $this->visibility_text =  (($this->visibility) == 1)? 'enabled' : 'disabled');
        parent::afterFind ();   // Call the parent's version as well
    }

    ...
}

This will effective give you a new field, so you can do something like :

$eventTypeModel = ManageEventsType::model()->findByPK($eventTypeId);
echo 'The visibility is .'$eventTypeModel->visibility_text;

So you final code will look like this.

<?php $this->widget('zii.widgets.CDetailView', array(
                    'data'=>$model,
                    'attributes'=>array(
                        'id',
                        'eventstype',
                        'visibility_text',     // <== show the new field ==> //
                        'enable',
                    ),
                ));
?>