Hi,
I have a problem with related models that has a relation to itself. I have a category table which simulate a tree.
I have a table category_cat where the id_cat is the category id and idcat_cat is the parent of the category and is related to the same table. The category table has a related table where we keep the translation of the category:
Table category_cat:
id_cat -> primary key
idcat_cat -> the category parent (relation to the same table to id_cat)
Table category_translations:
language -> the language id
id_cat_lang -> the category id (relation to category_cat)
name_cat_lang -> the name of the category
class Category extends \yii\db\ActiveRecord {
public function getCategory() {
return $this->hasOne(Category::className(), ['idcat_cat' => 'id_cat'])->from(Category::tableName().' as parentCategory') ;
}
public function getTranslate() {
$lang = $_SESSION['language']; # the active language from website
return $this->hasOne(CategoryTranslate::className(), ['id_cat_lang' => 'id_cat'])->onCondition(['language' => $lang]);
}
}
class CategoryTranslate extends \yii\db\ActiveRecord {
}
I want to make a query to get into a single sql the category with his parent and the translations of the category and the parent:
Category::find()->joinWith('category', 'category.translate', 'parentCategory', 'parentCategory.translate')->all()
But I get an error because of using same translate table without alias.
How can we use custom alias with joinWith or to add a dynamic alias?
Thank you.