Yii2:如何声明两个属性中的任何一个与 id 匹配的多对多关系?
Yii2: How to declare a has-many relation where either of two attributes match an id?
模型 Team
有两个 有很多关系 到 Game
:
public function getGamesWhereTeamIsSetAsHome()
{
return $this->hasMany(Game::className(), ['teamHome' => 'id']);
}
public function getGamesWhereTeamIsSetAsAway()
{
return $this->hasMany(Game::className(), ['teamAway' => 'id']);
}
我想要一个有很多关系 returns 所有游戏,其中 teamHome 或 teamAway 设置为团队的 id(比如以上两个关系)。
public function getGames()
{
return /* code here */;
}
如何建立这样的关系?
public function getGames($id)
{
return Games::find()->where(['or',['teamHome'=>$id],['teamAway'=>$id]])->all();
}
并在调用时
$games = $model->getGames($model->id);
模型 Team
有两个 有很多关系 到 Game
:
public function getGamesWhereTeamIsSetAsHome()
{
return $this->hasMany(Game::className(), ['teamHome' => 'id']);
}
public function getGamesWhereTeamIsSetAsAway()
{
return $this->hasMany(Game::className(), ['teamAway' => 'id']);
}
我想要一个有很多关系 returns 所有游戏,其中 teamHome 或 teamAway 设置为团队的 id(比如以上两个关系)。
public function getGames()
{
return /* code here */;
}
如何建立这样的关系?
public function getGames($id)
{
return Games::find()->where(['or',['teamHome'=>$id],['teamAway'=>$id]])->all();
}
并在调用时
$games = $model->getGames($model->id);