Relations: Through problem - wrong on clause in sql generated

Trying to get a simple through relation to work:

Country (1:n) Address (1:n) Location

relations in Location:




	public function relations() {

		return array(

			'address' => array(self::BELONGS_TO, 'Address', 'address_id'),

		);

	}



relations in Address:




	public function relations() {

		return array(

			'country' => array(self::BELONGS_TO, 'Country', 'country_id'),

			'locations' => array(self::HAS_MANY, 'Location', 'address_id'),

		);

	}



relations in Country:




	public function relations() {

		return array(

			'addresses' => array(self::HAS_MANY, 'Address', 'country_id'),

			'locations'=>array(self::HAS_MANY,'Location','country_id','through'=>'addresses'),

		);

	}



On lazy loading "Country.locations" following sql query is generated:




SELECT `locations`.`id` AS `t1_c0`, `locations`.`name` AS `t1_c1`, `locations`.`address_id` AS `t1_c2`, `locations`.`short_name` AS `t1_c3`, `locations`.`main_area_id` AS `t1_c4` FROM `location` `locations`  LEFT OUTER JOIN `address` `addresses` ON (`addresses`.`country_id`=`locations`.`id`) WHERE (`addresses`.`country_id`=:ypl0). Bound with :ypl0='1'



The ON clause in the left outer join is wrong. Correct would be addresses.id=locations.address_id.

Is this through definition incorrect or not possible in my relation scenario?

So did you already try this?




'locations'=>array(self::HAS_MANY,'Location','address_id','through'=>'addresses'),



/Tommy

Yes, that was my inital try (and expected from the docs) and leads to:

The relation "addresses" in active record class "Country" is specified with an invalid foreign key "address_id". There is no such column in the table "address".

Attached the relevant schema: 2365

schema.PNG

Have a look here what may solve your problem in 1.1.9

/Tommy

Great, thanks! For the record: Here is the according code fix. Because the array declaration will only work from version 1.1.9 (not just a matter of documentation).