ActiveRecord relations with parameters

Hi,

I have 2 models:




class Page extends \yii\db\ActiveRecord {

   ....

    public function getTranslations()

    {

        return $this->hasMany(PageTranslate::className(), ['id_page' => 'id_page_translate']);

    }    

}

class PageTranslate extends \yii\db\ActiveRecord {

  ....

}




The language field from pageTranslate table is language_code

I want to get all pages with a specific language with a relation with parameters something like:

Page::find()->translate(‘en’)->all()

The sql query created should be like this:

select * from pages left join pages_translation on pages.id_page = pages_translation.id_page_translate and pages_translation.language_code = ‘en’

How could I do this?

Thank you

Hi,

I found a solutions for this but I got another problem:

I made a customActiveQuery for Page model and I rewrote the find function for Page model

add in page model:




public function find() {

  return new PageActiveQuery(get_called_class());


}

/* I added a new relatioin to PageTranslation than get a single object, not an array of objects as getTranslations does

*/

public function getTranslate() {

  return $this->hasOne(PageTranslate::className(), ['id_page' => 'id_page_translate']);


}



Then create the new ActiveQuery:




class PageActiveQuery extends \yii\db\ActiveQuery {

		public function translate($lang) {			

			$this->joinWith([

					'translation' => function ($query) use ($lang) {

						$query->onCondition(['language_code' => $lang]);

					}

				]);

			return $this;

		}   

}

Now I can call:


Page::find()->translate('en')->all()

These problem beeing solved I got a new problem. Let’s supose that Page has a category related model that has a translation too:




class Category extends \yii\db\ActiveRecord {

   public function getTranslation() {

      return $this->hasOne(CategoryTranslate::className(), ['id_category' => 'id_category_translate']);

   }

   public function getTranslations() {

        return $this->hasOne(CategoryTranslate::className(), ['id_category' => 'id_category_translate']);

   }

}


class CategoryTranslation extends \yii\db\ActiveRecord {

}



in the Page model defined in the previous post I added:




   public function getCategory() {

        return $this->hasOne(Category::className(), ['id_page_category' => 'id_category']);

   }



I made a similar ActiveQuery for Category as I did for Page, but I don’t know how to call the relations so I will get a sql with all pages with an specific translation and the category of the page with related translation. I will expect something like this:


select * from pages 

left join page_translation on id_page = id_page_translate and page_translation.language_code='en' 

left join categories on id_category = id_page_category 

left join category_translation on id_category = id_category_translate and category_translation = 'en'



I tried: Page::find()->language(‘en’)->joinWith(‘category’)->language(‘en’) but it didn’t work.

How could I make the call?

Thank you