Using related attributes with same name in view

What is the appropriate way to display attributes from a related model in a view that have the same name as attributes in the parent object?

Consider a User class which BELONGS_TO another User being the referrer:

So in the User model we have:


public function relations()

{

	return array(

		'referrer' => array( self::BELONGS_TO, 'User', 'referrer_id')		// Has zero or one referrer

	);

}

This seems to work in the view, passing a User object as $model:


<?php echo CHtml::activeLabelEx($model->referrer,'company', array( 'label'=>'Referrer', 'required'=>false ) ); ?>

<?php echo CHtml::activeTextField($model->referrer, 'company', array( 'readonly'=>'readonly' )); ?>



After submitting the view the POST array contains the company attribute, but it has the value of the related User object (=referrer). The User.company attribute is of course also on the form. So we have two company fields on the form of which the last ends up in POST. If I were to store the model it would overwrite the User.company with referrer.company.

Clearly we need to distinguish between the two company fields. How is that done?

Again, any help is much appreciated.

Cheers

So you have two input fields




<?php echo CHtml::activeTextField($model, 'company', array( 'readonly'=>'readonly' )); ?>

<?php echo CHtml::activeTextField($model->referrer, 'company', array( 'readonly'=>'readonly' )); ?>



The problem is that by current design the generated "id" and "name" is the same for these two…

As you are only displaying the referrer company (readonly) you can use this




<?php echo CHtml::activeTextField($model->referrer, 'company', array( 

       'readonly'=>'readonly', 

       'id'=>'xx', 

       'name'=>'xx' 

 )); ?>



I wonder, why you use a textField at all. You only want to display the referrer user, right?


<?php echo CHtml::activeTextField($model->referrer, 'company', array( 'readonly'=>'readonly' )); ?>

So how about simply:


<?php echo $model->referrer->company ?>

Great, this will do!

MDomba’s suggestion does the trick, also added a ‘for’ option to the activeLabel.

Mike’s suggestion makes sense of course, thanks for that. I used the field so the styling would be the same as the input fields. But of course I can make the necessary changes to the css and add a class to resemble the input styles.

Thanks again

You can also use a non-active field (== not related to a model attribute):


CHtml::textField('someName',$model->referrer->company,array('readonly'=>'readonly'));

Even better :)

Another thing I ran into: If there’s no related record based on the BELONGS_TO relation, the value of the related field is NULL.

This basically ruins the layout of the page; it seems that the view is rendered without its surrounding template.

Btw: I’m using the above suggested CHtml::textField by now.

Should I programmatically check for NULL values or is there a smart way to cope with this?

You should always check any objects that could be null before accessing attributes. My preferred style for this is:


CHtml::textField('someName',$model->referrer===null ? '' : $model->referrer->company,array('readonly'=>'readonly'));

It’s a matter of taste wether you like this or not :)

Ok for my taste, works fine :rolleyes:

Using an activeLabelEx with the field still messed things up so I replaced it with a simple label.

All’s fine now, thanks for the help!