Model Reations

Model Relations

I’ ve got a bit complex situation and I cannot quite understand the behavior

I have the following models :

For table languages with pk language_id


class Languages extends CActiveRecord

{

	..

	public function relations()

	{

		return array();

	}

	..

}



For table contents_text with pk (and fk) content_id & language_id


class ContentsText extends CActiveRecord

{

	..

	public function relations()

	{

		return array(

			'languages' => array(self::HAS_ONE, 'Languages', 'language_id'),

		);

	}

	..

} 

For table contents with pk content_id


class Contents extends CActiveRecord

{

	..

	public function relations()

	{

		return array(

			...

			'text'	=> array(self::HAS_ONE, 'ContentsText', 'content_id'),

		);

	}

	..


	public function fetch($parent_id, $code)

	{

		return $this->with(array(

			'text'=>array(

				'with'=>'languages',

				'condition'=>'`code`=\'en\''

			),

		))->findAll('`parent_id`=\''.$parent_id.'\'');

	}

}

When function fetch is executed I would expect somthing like :

[sql]…

FROM contents

LEFT OUTER JOIN `contents_text`


	LEFT OUTER JOIN `languages`


		ON `languages`.`language_id` = `contents_text`.`language_id`


ON `contents_text`.`content_id` = `contents`.`content_id`

…[/sql]

but I get

[sql]…

FROM contents t

LEFT OUTER JOIN `contents_text` `text` ON (`text`.`content_id`=`t`.`content_id`) 


LEFT OUTER JOIN `languages` `languages` ON (`languages`.`language_id`=`text`.`content_id`) 

…[/sql]

What am I missing?

( Windows 7 Home Premiun, Apache 2.0.64, PHP 5.2.4, MySQL 5.1, Yii v1.1.8 )

I think two query are the same. Change only name of tables. no!?

They are not the same. In the first query the join with table languages is on

[sql]languages.language_id = contents_text.language_id[/sql]

and in the next is on

sql[/sql]

An this is my problem. I think that last content_id should be [i]language_id[/i] since i join "text" with "languages"

Try this




'languages' => array(self::BELONGS_TO, 'Languages', 'language_id'),



/Tommy

Thanks a lot!

It works.