Hi,
for several hours I have been trying to find out why relation that should work returns nothing.
So I have 2 models:
- Planet
public function relations()
{
return array(
'galaxy' => array(self::HAS_ONE, 'Galaxy', 'id_planet'),
);
}
- Galaxy
public function relations()
{
return array(
'planet' => array(self::BELONGS_TO, 'Planet', 'id_planet'),
);
}
In database in these 2 tables I have rows related with each other:
Planet->id = 1006
Galaxy->id = 9
Galaxy->id_planet = 1006
And in controller, just for test i set up the following:
$galaxy = Galaxy::model()->with('planet')->findByPk(9);
echo $galaxy->id; // returns 9
echo $galaxy->planet->id; // returns 1006
$planet = Planet::model()->with('galaxy')->findByPk(1006);
echo $planet->id; // returns 1006
echo $planet->galaxy->id; // returns error: "Trying to get property of non-object"
My question is what could be the cause that $planet->galaxy is empty?