我在数据库中有相关表,如下图所示:
然后,我有页面可以查看我的文件,如下图所示:
我想显示它的用户名,而不是 ID。 这是我在 view/file/_view.php 中的代码
<div class="view">
<b><?php echo CHtml::encode($data->getAttributeLabel('id_file')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->id_file),array('view','id'=>$data->id_file)); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('nama_file')); ?>:</b>
<?php echo CHtml::encode($data->nama_file); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('deskripsi')); ?>:</b>
<?php echo CHtml::encode($data->deskripsi); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('id_user')); ?>:</b>
<?php echo CHtml::encode($data->id_user); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('id_kategori')); ?>:</b>
<?php echo CHtml::encode($data->id_kategori); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('tgl_post')); ?>:</b>
<?php echo CHtml::encode($data->tgl_post); ?>
<br />
<hr />
</div>
文件 Controller .php
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
输出_view.php的index.php
<?php
$this->breadcrumbs=array(
'Files',
);
?>
<h1>Files</h1>
<?php $this->widget('bootstrap.widgets.TbListView',array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
你能帮我改一下吗?多谢。 P.S.:抱歉我的英语不好
最佳答案
首先,您需要与模型中的用户表建立关系,我假设您已经有了,因为您在上图中有 FK。让我们假设关系是“用户”,并且在用户表中要显示的字段是“用户名”。
您应该替换:
php
<?php echo CHtml::encode($data->id_user); ?>
与:
php
<?php echo CHtml::encode($data->user->username); ?>
关于php - Yii - 如何显示其他属性而不是其 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22603055/