查看相关表的另一个属性-yii

查看相关表的另一个属性-yii,yii,yii-components,Yii,Yii Components,我想在用户配置文件视图中显示country name而不是country id(country id在用户配置文件tbl中是FK) 有人能帮我吗 这是我的视图控制器/操作,其中我还有国家/地区模型: public function actionView($id) { $model = new TblUserProfile; $model = TblUserProfile::model()->find('user_id=:user

我想在用户配置文件视图中显示
country name
而不是
country id
country id
在用户配置文件tbl中是FK)

有人能帮我吗

这是我的视图控制器/操作,其中我还有国家/地区模型:

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>

使用以下代码:

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

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

使用以下代码:

<b><?php echo CHtml::encode($data->country->getAttributeLabel('name')); ?>:</b>
<?php echo CHtml::encode($data->country->name); ?>
<br />
请阅读,了解如何更改更多内容。

我会使用:

$model = TblUserProfile::model()->with('country')->find('user_id=:user_id', array(':user_id' => $id));
$model->country->CountryName;
解释为('country'):字符串country来自数组关系键

然后要在视图中显示,请使用:

$model = TblUserProfile::model()->with('country')->find('user_id=:user_id', array(':user_id' => $id));
$model->country->CountryName;
  • $model包含
  • country包含由关系外键连接的国家的“模型”
  • CountryName是Country表中的列名
这是以MVC方式推荐的方法。我的示例和上面的示例都可以工作,但是上面的示例在视图中有更多的过程/逻辑决策,这打破了MVC的概念,MVC中的模型应该是胖模型,并包含所有可能的逻辑/处理,然后控制器将提取此数据并提供给视图。

我将使用:

$model = TblUserProfile::model()->with('country')->find('user_id=:user_id', array(':user_id' => $id));
$model->country->CountryName;
解释为('country'):字符串country来自数组关系键

然后要在视图中显示,请使用:

$model = TblUserProfile::model()->with('country')->find('user_id=:user_id', array(':user_id' => $id));
$model->country->CountryName;
  • $model包含
  • country包含由关系外键连接的国家的“模型”
  • CountryName是Country表中的列名

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

您也可以遵循以下答案: 只需将下面的代码放入
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 ,
            ],

您也可以按照以下答案进行操作: 只需将下面的代码放入
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 ,
            ],


你能显示此视图的配置/代码吗?好的,我正在编辑我的问题。还需要userprofile模型中的关系吗?你能显示此视图的配置/代码吗?好的,我正在编辑我的问题。还需要userprofile模型中的关系仍然显示id,我说这是因为我有view.php,我在这里传递的是country_id:我告诉过你,根据你在问题中显示的代码(你显示了_视图和非视图),如果你谈论的是细节视图,那么解决方案将略有不同。这为我节省了很多时间。如果模型和数据库中存在正确的关系,那么我们可以很容易地访问这样的属性。仍然显示id,我说这是因为我有view.php,其中我传递了country_id:我根据您在问题中显示的代码告诉您(您显示了_view而不是view),如果你说的是细节视图,那么解决方案会略有不同。这为我节省了很多时间。如果模型和数据库中存在正确的关系,那么我们可以很容易地访问这样的属性。实际上,您不需要使用进行,因为这只会进行快速加载。另外,我在视图中所做的一切就是描述视图应该是什么样子,当前字段应该是什么样子,所以,imho,我认为我没有违反任何mvc规则。你可以看看我包含的文档链接,看看如何使用
CDetailView
中的
attributes
,显示、设置字段样式等。ps:我只是想为我的答案辩护别担心,我也使用这两种方法(你的和我的),但我尝试从两种好方法中挑选出更好的,什么时候更好的好,OK,酷,我刚才还说它不会破坏任何mvc规则实际上你不需要
,因为这只会进行快速加载。另外,我在视图中所做的一切就是描述视图应该是什么样子,当前字段应该是什么样子,所以,imho,我认为我没有违反任何mvc规则。你可以看看我包含的文档链接,看看如何使用
CDetailView
中的
attributes
,显示、设置字段样式等。ps:我只是想为我的答案辩护别担心,我也使用这两种方法(你的和我的),但我尝试从两种好方法中挑选出更好的,什么时候更好的好,OK,酷,我也只是说它不会违反任何mvc规则