如何使用yii显示查找表中分配给整数的值


How do you show the value assigned to an integer from a lookup table with yii?

在我的表单中,我通过从表中填充值来创建值。

<?php echo $form->dropDownList($model,'status', CHtml::listData(Statusprospect::model()->findAll(), 'id', 'status'),array('prompt' => 'Select')); ?>

当我查看记录时,它有一个1,因为它应该用于状态。我如何使它在查看记录时显示值,而不是1.

当前显示该字段的视图文件代码如下:

    <?php echo CHtml::encode($data->status); ?>

模型确实定义了关系:

    public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'status0' => array(self::BELONGS_TO, 'Statusprospect', 'status'),
    );
}

我如何完成显示值而不是数字?

现在这应该工作$data->status0->status

注意如果$data->status可以为null,则可能没有设置$data->status0->status,因此如果是这种情况,请事先检查。你可以使用

CHtml::encode(isset($data->status0->status) ? $data->status0->status : '-');