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