0

是否可以在 yii2 中声明与同一张表的 2 个关系?

例子:

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

所以这里是模式,正如你所看到的,一个游戏应该由 2 个团队组成,team1 和 team2。

现在在 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 回答