Is it possible to declare 2 relation to the same table in yii2?
Example:
tournament(id, name)
game(id, tournamentId, team1Id, team2Id)
team(id, name)
player(id, name, teamId)
So here is the schema and as you can see, a game should consist of 2 team, team1 and team2.
Now in the Tournament Model in yii, i need to create a relation which would give me the teams who participated in the tournament.
public class Tournament extends ActiveRecord
{
...
public function getGames()
{
return $this->hasMany(Game::className(), ['tournamentId' => 'id']);
}
public function getParticipatedTeams()
{
return $this->hasMany(Team::className(), [/*what should the link be? */])->via('games');
}
}
How do i get the teams from the tournament model??
public class Tournament extends ActiveRecord
{
...
public function getGames()
{
return $this->hasMany(Game::className(), ['tournamentId' => 'id']);
}
public function getParticipatedTeam1()
{
return $this->hasMany(Team::className(), ['id' => 'team1Id'])->via('games');
}
public function getParticipatedTeam2()
{
return $this->hasMany(Team::className(), ['id' => 'team2Id'])->via('games');
}
}