Answer the question
In order to leave comments, you need to log in
How to work with linked tables in Yii2?
I have tables:
User
(id, name, surname)
and
UserContacts
(u_id1, u_id2).
Action in the model:
(Does not work)
return $this->hasMany(User::className(), ['id' => 'u_id1', 'id' => 'u_id2'])
->viaTable('UserContacts', ['id' => 'u_id1', 'id' => 'u_id2']);
Tell me how can I get a linked table, where each u_id1 = id (from the User table), u_id2 = id (from the User table), with a single query using viaTable ()?
And then get all the data.
Answer the question
In order to leave comments, you need to log in
public function getAttrs()
{
return $this
->hasMany(Profile::className(), ['user_id' => 'id'])
->where('name = "name"')->union(
$this->hasMany(Profile::className(), ['user_id' => 'id'])->where('name = "surname"')
);
}
return $this->hasMany(User::className(), ['id' => 'u_id1'])
->viaTable('UserContacts', ['id' => 'u_id1'])->union($this->hasMany(User::className(), ['id' => 'u_id2'])
->viaTable('UserContacts', ['id' => 'u_id2']));
return $this->hasMany(User::className(), ['id' => 'u_id2'])
->viaTable(UserContact::tableName(), ['u_id1' => 'id']);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question