cDetailview显示别名yii


cDetailview Display alias name yii

我的视图代码

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

控制器代码

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

在我的视图页面记录显示如下

Id          3
Eventstype  Holiday
Visibility  2
Enable      0

我想显示可见性为启用或禁用。1-启用,2-禁用,你知道

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

更"优雅"的方法是改变你的ActiveRecord模型。

class ManageEventsType extends CActiveRecord
{
   /* Give it a name that is meaningful to you */
   public $visibility_text;
   ...
}

这会通过创建一个额外的属性来扩展你的模型。

在模型中,然后添加(并覆盖)afterFind()函数。

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
    }
    ...
}

这将有效地给你一个新的字段,所以你可以这样做:

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

最后的代码是这样的

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