首页 文章

Yii2:如何声明一个有多个关系,其中两个属性中的任何一个匹配一个id?

提问于
浏览
0

模型 TeamGame 有两个有很多关系:

public function getGamesWhereTeamIsSetAsHome()
{
    return $this->hasMany(Game::className(), ['teamHome' => 'id']);
}

public function getGamesWhereTeamIsSetAsAway()
{
    return $this->hasMany(Game::className(), ['teamAway' => 'id']);
}

我想要一个有很多关系,返回所有游戏,其中teamHome或teamAway设置为团队的id(就像上面两个关系的组合) .

public function getGames()
{
    return /* code here */;
 }

我如何 Build 这样的关系?

1 回答

  • 0
    public function getGames($id)
    {
       return Games::find()->where(['or',['teamHome'=>$id],['teamAway'=>$id]])->all();
    }
    

    在打电话的时候

    $games = $model->getGames($model->id);
    

相关问题