Related data in textArea

Hi all,

I’m a newbie with YII and I can’t seem to figure out how to display a related field in a textArea.

In my Managers model I have:


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(

                      'jobs' => array(self::HAS_MANY, 'Job', 'manager_ref'),

		);

	}

In my Jobs model I have:


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(

			'managerRef' => array(self::HAS_ONE, 'Managers', 'manager_ref'),

		);

	}

In my job/view update form:


<div class="row">

		<?php echo $form->labelEx($model,'manager_ref'); ?>

		<?php echo $form->dropDownList($model,'manager_ref', CHtml::listData(Managers::model()->findAll(), 'manager_ref', 'full_name')); ?>

		<?php echo $form->error($model,'manager_ref'); ?>

	</div>


<div class="row">

		<?php echo "Manager Address"; ?>

		<?php echo $form->textArea($model, $model->managerRef->address, array('rows'=>5, 'cols'=>75)); ?>

		<?php echo $form->error($model,'manager_ref'); ?>

	</div>

Gives “trying to get property of non-object”


<?php echo $form->textArea($model, 'managerRef->address', array('rows'=>5, 'cols'=>75)); ?>

Gives “Property "Job.managerRef->address" is not defined.”

The drop down list works fine displaying the correct Manager for each Job.

I’m probably missing something really simple but, is what I’m trying to do possible?

Any help would be greatly appreciated.

Cheers,

Mike T

Just a few ideas which may guide you to the answer:

  1. spelling - check if address is spelled exactly the same in your db

  2. try echoing different column

  3. does the error happen while editing an existing entry or when trying to create a new one? When you create a new job there is no any related managerRef so trying to access one will return null. Therefore address is a property of non-object.

4 try echoing address using CHtml::value. This function checks if property exist and if not it displays a default text (initially empty string).

Thanks for the suggestion Sidewinder…


echo CHtml::value($model, 'managerRef.address', 'JUNK');

Gives the address OK, but


echo $form->textArea($model, 'managerRef->address', array('rows'=>5, 'cols'=>75)); ?>

gives "Property "Job.managerRef->address" is not defined."

Am I using the correct format of ‘managerRef->address’ in the textArea declaration?

Or failing that, how do I get the result of CHTML::VALUE into the textArea?

Cheers,

Mike T

Since textArea expects second argument to be a string and value returns a string it should be as easy as:




echo $form->textArea($model,CHtml::value($model, 'managerRef.address', 'JUNK'), array('rows'=>5, 'cols'=>75)); ?>



if you want it to be a one liner, or you can always split it like that:




$address =  CHtml::value($model, 'managerRef.address', 'JUNK'); 

echo $form->textArea($model,$address, array('rows'=>5, 'cols'=>75)); ?>



if you want to keep code clean.

I’m afraid not.

that gives:


Property "Job.Address Line 1" is not defined.

where “Address Line 1” is the Manager’s address.

Why is it looking for it as a property? Am I missing something else? I’ve reread a couple of tutorials and from what I can understand, it should work…

Actually, I’ve just figured, it’s not the relation that’s the problem!

textArea (or textField for that matter) doesn’t appear to accept a string as its’ value. I’ve put in a random string and I get the same result. Is it possibly the way I’m declaring things? Is it even possible to put a string into the value in textArea?

My bad. textField takes string as a second parameter but it’s not a value but property name. Just like CHtml::Value. Try this:


echo $form->textArea($model, 'managerRef.address', array('rows'=>5, 'cols'=>75)); ?>