Answer the question
In order to leave comments, you need to log in
How to link across 2 tables in YII2?
there are 3 models:
1. User
2. Organization
3. Terminal
User and Organization are connected through the table l_user_organization
Accordingly, in User the connection is described as follows
public function getRelOrganizations()
{
return $this->hasMany(Organization::className(), ['id' => 'organization_id'])->viaTable('{{%l_user_organization}}', ['user_id' => 'id'])->orderBy(['name_ru'=>SORT_ASC]);
}
public function getRelTerminals()
{
return $this->hasMany(Terminal::className(), ['organization_id' => 'id'])->orderBy(['name'=>SORT_ASC]);;
}
foreach(User::find()->where(['id'=>1])->one()->relOrganizations AS $organization) {
print_r($organization->relTerminals);
}
print_r(User::find()->where(['id'=>1])->one()->relTerminals);
User::find()->where(['id'=>1])->one()->getRelOrganizations()->asArray();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question