I am new to YII2 and I have a problem with my relationships:
I have Users and Category. They have a m-m relationship. Now i would like to see the Categories a user has. For that I made a table named "user_category" which looks as follows:
In my models i have the following code as suggested in How do I work with many-to-many relations in Yii2 :
public function getUsers(){
return $this->hasMany(TabUser::className(), ['intUserID' => 'intUserID'])
->viaTable('user_category', ['intCategoryID' => 'intCategoryID']);
}
public function getCategories(){
return $this->hasMany(TabCategory::className(), ['intCategoryID' => 'intCategoryID'])
->viaTable('user_category', ['intUserID' => 'intUserID']);
}
Then i linked them together:
if($user->validate()) {
$user->link('categories', $category);
}
var_dump($user->getCategories());
But this does not return my categories it returns the following:
Does anybody know what I do wrong?
Thanks for your time and help!!