Related Many Many Sql Error

Getting this error from giix generated crud:


CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'contacts_contacts. contact_id' in 'on clause'. The SQL statement executed was: SELECT `contacts`.`id` AS `t1_c0`, `contacts`.`firstname` AS `t1_c1`, `contacts`.`lastname` AS `t1_c2`, `contacts`.`salutation` AS `t1_c3`, `contacts`.`title` AS `t1_c4`, `contacts`.`notes` AS `t1_c5`, `contacts`.`telephone` AS `t1_c6`, `contacts`.`mobile` AS `t1_c7`, `contacts`.`switchboard` AS `t1_c8`, `contacts`.`fax` AS `t1_c9`, `contacts`.`address1` AS `t1_c10`, `contacts`.`address2` AS `t1_c11`, `contacts`.`address3` AS `t1_c12`, `contacts`.`address4` AS `t1_c13`, `contacts`.`address5` AS `t1_c14`, `contacts`.`postcode` AS `t1_c15`, `contacts`.`region` AS `t1_c16`, `contacts`.`user_id` AS `t1_c17`, `contacts`.`company_id` AS `t1_c18`, `contacts`.`create_time` AS `t1_c19`, `contacts`.`create_user_id` AS `t1_c20`, `contacts`.`update_time` AS `t1_c21`, `contacts`.`update_user_id` AS `t1_c22` FROM `tbl_contact` `contacts` INNER JOIN `tbl_attribute_assignment` `contacts_contacts` ON (`contacts_contacts`.` contact_id`=:ypl0) AND (`contacts`.`id`=`contacts_contacts`.`attribute_id `). Bound with :ypl0='2'

The relationship code:




	public function relations() {

		return array(

			...

			'attributes' => array(self::MANY_MANY, 'Attribute', 'tbl_attribute_assignment( attribute_id, contact_id )'),

		);

	}



and the view:




	foreach($model->attributes as $relatedModel) {

		echo GxHtml::link(GxHtml::encode(GxHtml::valueEx($relatedModel)), array('attribute/view', 'id' => GxActiveRecord::extractPkValue($relatedModel, true)));

	}



What am i doing wrong?

Should I not be using Gii to generate code for many_many relationships? Tried some of the other templates (Awecrud) and they failed with errors too.

There’s a space here:




`contacts_contacts`.` contact_id`



And the cause is at:




'attributes' => array(self::MANY_MANY, 'Attribute', 'tbl_attribute_assignment( attribute_id, contact_id )')



Remove the spaces and let me know whether it works:




'attributes' => array(self::MANY_MANY, 'Attribute', 'tbl_attribute_assignment(attribute_id,contact_id)'),



Was the code generated like this or did you (or your IDE) add those spaces?