简体   繁体   中英

cDetailview Display alias name yii

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

$text = $model->visibility == 1 ? 'enable' : 'disabled';

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

    ),
)); ?>

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',
                    ),
                ));
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM