0

我想显示country name而不是country id在用户个人资料视图中显示(country id在用户个人资料 tbl 中是 FK)。

任何人都可以帮助我吗?

这是我的视图控制器/动作,其中我也有 Country 模型:

public function actionView($id) 
            {

        $model = new TblUserProfile;

        $model = TblUserProfile::model()->find('user_id=:user_id', array(':user_id' => $id));


      $countrymodel =  new Country;

       $countrymodel = Country::model()->findAll();
//       var_dump($countrymodel->name);
//       die();
        $this->render('view', array(
            'model' => $model ,
            'country' =>$countrymodel

        ));
    }

这是我的看法

<div class="view">

    <b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
       <?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

    <br />

    <b><?php echo CHtml::encode($data->getAttributeLabel('user_id')); ?>:</b>
    <?php echo CHtml::encode($data->user_id); ?>
    <br />


    <b><?php echo CHtml::encode($data->getAttributeLabel('user_occuption')); ?>:</b>
    <?php echo CHtml::encode($data->user_occuption); ?>

    <br />
    <b><?php

     // $model = TblUserProfile::model()->find('country_id=:country_id', array(':user_id' => $id));


//echo CHtml::encode($model->getAttributeLabel('country')); ?>:</b>
    <?php// echo CHtml::encode($model->name);

        ?>

    <br />

</div>

现在我想在上面的视图中显示国家名称。

这些是国家和用户资料表的关系

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(
            'user' => array(self::BELONGS_TO, 'TblUser', 'user_id'),
            'country' => array(self::BELONGS_TO, 'country', 'country_id'),
            'state' => array(self::BELONGS_TO, 'state', 'state_id'),
            'city' => array(self::BELONGS_TO, 'city', 'city_id')

        );
    }
4

3 回答 3

3

使用以下代码:

<b><?php echo CHtml::encode($data->country->getAttributeLabel('name')); ?>:</b>
<?php echo CHtml::encode($data->country->name); ?>
<br />

假设在您的国家模型中,国家名称列是name.

由于关系已经到位,我们可以使用它来访问每个用户的相关国家/地区。

在详细视图中,它将更改为:

// instead of 'country_id'
array(
    'name'=>'country_id',
    'value'=>$model->country->name
)

阅读CDetailView文档以了解如何更改更多内容。

于 2012-10-15T08:32:02.623 回答
2

我会使用:

$model = TblUserProfile::model()->with('country')->find('user_id=:user_id', array(':user_id' => $id));

with('country') 解释:字符串 country 来自于数组关系键。

然后在视图中显示您将使用:

$model->country->CountryName;
  • $model 包含
  • country 包含由关系外键连接的国家的“模型”
  • CountryName 是 Country 表中的列名

这是以 MVC 方式进行的推荐方法。我的示例和上面的示例都有效,但是上面的示例在视图中有更多的过程/逻辑决策,这打破了 MVC 的概念,其中模型应该是胖模型并包含所有可能的逻辑/处理,然后控制器将拉取这个数据并将其提供给视图。

于 2012-10-15T11:22:00.177 回答
0

您也可以按照以下答案进行操作:只需将以下代码放入您的view.php文件中

            [
                "attribute"=>"User_Country_Id",
                'value' =>$model->country->conName ,
            ],
            [
                "attribute"=>"User_State_Id",
                'value' =>$model->states->stsName ,
            ],
            [
                "attribute"=>"User_City_Id",
                'value' =>$model->cities->ctName ,
            ],
于 2016-11-09T09:51:58.767 回答