Data from foreign table is not returned relational AR

I am currently working on a form which involves 2 tables, for example:




Table parent:

+-------------+---------+------+-----+---------+----------------+

| Field       | Type    | Null | Key | Default | Extra          |

+-------------+---------+------+-----+---------+----------------+

| id          | int(11) | NO   | PRI | NULL    | auto_increment |

|                      ** other columns **                      |

+-------------+---------+------+-----+---------+----------------+




Table child:

+-------------+---------+------+-----+---------+----------------+

| Field       | Type    | Null | Key | Default | Extra          |

+-------------+---------+------+-----+---------+----------------+

| id          | int(11) | NO   | PRI | NULL    | auto_increment |

| parentId    | int(11) | NO   | MUL | NULL    |                |

| status      | int(11) | YES  |     | NULL    |                |

+-------------+---------+------+-----+---------+----------------+



In model Parent, I added




public function relations()

{

	return array(

		/* other relational rules */

		'child' => array(self::HAS_ONE, 'Child', 'parentId'),

	);

}



And in model Child, I added




public function relations()

{

	return array(

		'parent' => array(self::BELONGS_TO, 'Parent', 'parentId'),

	);

}



Everything’s fine when retrieving rows from the [color="#4169E1"]parent[/color] table, but the [color="#4169E1"]status[/color] from the [color="#4169E1"]child[/color] table is undefined, though there’s data for the equivalent parent row. I already printed the array [font=“Courier New”][color="#0000FF"]$model[/color][/font] to check if there’s really data but [color="#4169E1"]status[/color] returned no data/undefined.

Any thoughts on this? Is it because of the bad relational AR? I really can’t understand why I’m having this problem.

Thank you for the help!

P.S. Using Yii 1.1 and I’m a newbie

UPDATE: Already working. Thanks to this topic, I had an idea on where the problem’s coming from. http://www.yiiframework.com/forum/index.php/topic/9834-cdetailview-and-model-relations/

For everyone who’ll encounter the same problem as mine in the future…

I assume there’s nothing wrong in the relational AR (because now it works lol), it has something to do with the fetching of data in [color="#4169E1"]_form.php[/color]. Previously, the code where I need the data from the foreign table looks like this:




<div class="row">

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

	<?php echo $form->checkBox($model, 'child'); ?>

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

</div>



I changed it to this:




<div class="row">

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

	<?php echo $form->checkBox($model->child, 'status'); ?>

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

</div>



And now it perfectly works. I wasted half a day solving this problem. I should really read the docs thoroughly…!

P.S. Yii is good stuff.

UPDATE 2.0: The solution above is wrong! I got an error saying, "Call to a member function isAttributeRequired() on null". Thanks to this link, http://www.larryullman.com/2010/08/10/handling-related-models-in-yii-forms/ I was enlightened on where my problem really is. I thought relational AR will automatically do the trick but well, it seems like I was wrong.

So, I reverted it back to




<div class="row">

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

	<?php echo $form->checkBox($model, 'child'); ?>

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

</div>



And In [color="#4169E1"]ParentController.php[/color] I added some lines in function loadModel()




public function loadModel($id)

{

	$model=Parent::model()->findByPk($id);

	$criteria=new CDbCriteria;

	$criteria->condition='parentId=:parentId';

	$criteria->select='status';

	$criteria->params=array(':parentId'=>$id);

	$parent=Child::model()->findAll($criteria);

	$model->parent = $parent[0]['status'];

	if($model===null)

		throw new CHttpException(404,'The requested page does not exist.');

	return $model;

}



Now it’s working fine…

[s]

P.S. For everyone who’ll be having the same problem as mine in the near future, refer to this post.

P.P.S. Why does no one reply to my queries? sad face

[/s]