[英]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']);
}
我想要一個具有很多關系的返回所有游戲的游戲,這些游戲都將teamHome或teamAway設置為team的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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.