Active Record Relation Alias

Hi,

i’m trying to set an alias to a relation by using the “with” method of ActiveRecord.

The problem is that i can only set the alias for the last element :


$ar->with(array('rel1.rel2' => array('alias' => 'rel2Alias'));

I have to retrieve the “rel1” and “rel2” for two line of the AC class so i must set a different alias for the “rel1” relation but i don’t know how to do.

Any suggestions ?

Dear Friend

I hope you are not averse in doing something like the follwoing.




$ar->with(array('rel1'=>array('alias' => 'rel1Alias'),'rel1.rel2' => array('alias' => 'rel2Alias')));



Regards.

Thanks for your answer.

I tested it but it is not working :

It says to me that "rel2" is not defined in model class.

In fact, your code says :


"retrieve rel1 as rel1Alias from MainModel AND rel2 as rel2alias from MainModel"

and i want something like


"retrieve rel1 as rel1Alias from MainModel AND rel2 as rel2alias from rel1Alias"

I do not want :

rel1 + rel2

but

rel1.rel2

and set alias on rel1

if found the solution by reading closely the documentation :


    $ar->with(array('rel1' => array('alias' => 'rel1Alias', 'with' => array('rel2' => array('alias' => 'rel2alias'))));

Dear Friend

Exactly.I am glad you have found out the solution.

I thought you have defined both relations in main model.

cheers.

ok, so i may need your help for something related :)

I have a Translation table which contains a foreign key to a language line of the current translation.

To retrieve the language code of each translation automatically, i set a "defaultScope" on the Translation model, which retrieve the language information with each Translation.

But an alias clash is coming when i try for example to retrieve the translation of the title and description of an article of my blog (example). Two translation have to be retrieve but the alias is the same.

Should i always define full relations with "with" methods and do not use "defaultScope()" to avoid this ?

Dear Friend

I am not able to comprehend the exact problem you are facing.

Can you please bit explain further with structuers of tables related.

Regards.

ok, here is the structure of the tables :

Language


  • id

  • code (like fr, en, nl)

Translation


  • id

  • value // "The" translation

  • fk_language // In this language

  • fk_translation_group

Translation_group


  • id

Article


  • id

  • fk_title_translation_group

  • fk_content_translation_group

This is the way i manage translations in my database.

So, i set a defaultScope() on Translation like this :




public function defaultScope() {

		return array(

			'with' => array(

				'fkLanguage'

			)

		);

	}

and then i want to fetch all articles with their title and content :


Article::model()->with('fkTitleTranslationGroup.translations', 'fkContentTranslationGroup.translations')->findAll();

// "translations" is the HAS_MANY relation to Translation table

This code throws SQL error because the same alias is used for relation "fkLanguage" for the two translations fetched.

ok, i found the solution :




public function defaultScope() {

        return array(

		'with' => array(

			'relationName' => array(

				'alias' => $this->getTableAlias(false, false).'relationName'

			)

		), 

	);

}