phamdoan
(Linuxdoanpham)
1
Hi all,I use hasMany but show error "backend\modules\categories\models\Categories has no relation named "category_descriptions"."
public function getCategoryDescriptions() {
return $this->hasMany(CategoryDescriptions::className(), ['category_id' => 'category_id']);
}
/**
* get category all.
* @return data.
*/
public function getAll(){
$query = Categories::find();
//$catalog = $query->all();
$catalog = $query->joinWith('category_descriptions')->all();
return $catalog;
}
you can help me !
It should be ‘categoryDescriptions’ and not ‘category_descriptions’.
$catalog = $query->joinWith('categoryDescriptions')->all();
amir.duran
(Amir Duran)
3
You are doing it wrong. If you already have an object of class Category, then you can get all category descriptions simply by calling:
$category = Category::findOne($id);
$category->categoryDescriptions;//This will return you all descriptions for category with ID=$id
Read this: http://www.yiiframework.com/doc-2.0/guide-db-active-record.html