Relation Working Only In 1 Direction.

Hi,

for several hours I have been trying to find out why relation that should work returns nothing.

So I have 2 models:

  1. Planet



public function relations()

{

	return array(

		'galaxy' => array(self::HAS_ONE, 'Galaxy', 'id_planet'),

	);

}



  1. 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?

This is what U wrote for DB data:

Planet->id = 1006

Galaxy->id = 9

Galaxy->id_planet = 1006

there is no Planet->id_planet.

Do U have id_planet field in Planet table/model.

Ok, I just found out, that I have a field named “galaxy” as well as relation so ye, Im stupid :D