0

I want to display country name instead of country id in user profile view( country id is FK in user profile tbl) .

anyone can help me with this ?

This is my view controller/action in which I have Country model as well:

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

        ));
    }

This is my view

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

Now I want to display country name in above view.

These are the relationships of country and user profile table

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')

        );
    }
Shumaila Khan
  • 91
  • 1
  • 5
  • 19

3 Answers3

3

Use the following code:

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

Provided that in your country model, the country name column is name.

Since the relation is in place we can use it to access the related country for each user.

In detailview it'll change to:

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

Read the CDetailView doc to see how you can change even more things.

bool.dev
  • 17,508
  • 5
  • 69
  • 93
  • 1
    Still displaying id , I guss this is becuase I have view.php , in which I am passing country_id : widget('zii.widgets.CDetailView', array( 'data' => $model, 'attributes' => array( // 'id', // 'user_id', 'country_id', 'state_id', 'city_id', 'user_dp', 'user_cover_photo', ), )); ?> – Shumaila Khan Oct 15 '12 at 08:41
  • i told you according to the code that you showed in your question (you have shown _view and not view), if you are talking about detail view then the solution will be slightly different – bool.dev Oct 15 '12 at 08:43
  • This saved me lot of time. If correct relations are there in the model and DB , then we can easily access the properties like this. – Buminda May 16 '13 at 08:54
2

I would use:

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

with('country') explained: the string country comes from the array relations key.

Then to display in the view you would use:

$model->country->CountryName;
  • $model contains
  • country contains a "model" of country joined by the relation foreign key
  • CountryName is the column name in the Country table

This is the recommended way to do it in the MVC way. Both my example and the one above work, but the one above has more process/logic decisions in the view, which breaks the concept of MVC where the Model should be the fat one and contain all logic/processing possible, then controller will pull this data and provide it to the view.

Marian Zburlea
  • 9,177
  • 4
  • 31
  • 37
  • actually you don't need `with`, coz that will only do eager loading. plus all i'm doing in the view is describing how the view should look, how the current field should look, so, imho, i don't think i'm breaking any mvc rules there. you can have a look at the documentation link that i included, to see how `attributes` in `CDetailView` can be used, to show, style, etc the fields. – bool.dev Oct 15 '12 at 11:36
  • ps: i'm just trying to defend my answer – bool.dev Oct 15 '12 at 11:45
  • 3
    No worries, I use both methods (yours and mine) too but I try to sort out from two good ones which is better and when is better – Marian Zburlea Oct 15 '12 at 12:14
  • 1
    ok, cool, i was also just saying that it doesn't break any mvc rule – bool.dev Oct 15 '12 at 12:19
0

You can also follow this below answer: Just put the below code in your view.php file

            [
                "attribute"=>"User_Country_Id",
                'value' =>$model->country->conName ,
            ],
            [
                "attribute"=>"User_State_Id",
                'value' =>$model->states->stsName ,
            ],
            [
                "attribute"=>"User_City_Id",
                'value' =>$model->cities->ctName ,
            ],
Bhatt Akshay
  • 159
  • 1
  • 14