0

yii2で同じテーブルに対して2つのリレーションを宣言することは可能ですか?

例:

tournament(id, name)
game(id, tournamentId, team1Id, team2Id)
team(id, name)
player(id, name, teamId)

スキーマは次のとおりです。ご覧のとおり、ゲームは team1 と team2 の 2 つのチームで構成される必要があります。

yii のトーナメント モデルでは、トーナメントに参加したチームを提供する関係を作成する必要があります。

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');
    }
}

トーナメント モデルからチームを取得するにはどうすればよいですか??

4

1 に答える 1

0
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');
    }
}
于 2015-11-30T12:40:35.960 に答える