我現在發現了Yii框架,並嘗試開發一個簡單的應用程序,該應用程序允許創建聯盟並將玩家分配給它。這些關係是多對多的(一個球員可以訂閱很多聯賽,一個聯盟包含很多球員)。所以,我有三個數據庫表 - tbl_league,tbl_player和tbl_league_player和我創建外鍵是這樣的:MANY_MANY關係不適用於我
ALTER TABLE tbl_league_player ADD FOREIGN KEY league_id_idxfk (league_id) REFERENCES tbl_league (id);
ALTER TABLE tbl_league_player ADD FOREIGN KEY player_id_idxfk (player_id) REFERENCES tbl_player (id);
現在我試圖用認購的所有玩家列表中顯示的聯賽,所以我在LeagueController的actionView說:
public function actionView($id)
{
$issueDataProvider = new CActiveDataProvider('Player', array(
'criteria' => array(
'condition' => 'league_id=:leagueId',
'params' => array(
':leagueId' => $this->loadModel($id)->id
),
),
'pagination' => array(
'pageSize' => 1,
),
));
$this->render('view',array(
'model'=>$this->loadModel($id),
'issueDataProvider' => $issueDataProvider,
));
}
我想在這裏做的是從訂閱到特定聯賽ID玩家表中獲取所有玩家。爲此,我有關係的人物模型是這樣定義的:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'League' => array(self::MANY_MANY, 'League', 'tbl_league_player(player_id, league_id)'),
);
}
的問題是,我去,例如,聯盟/視圖& ID = 2,我收到此錯誤:「SELECT COUNT(* )從tbl_player
t
WHERE league_id =:leagueId「因此看起來關係不起作用。我錯過了什麼?
非常感謝!
只是一個警告,儘量去除''tbl_league_player我以前見過一些怪事與空間(player_id,league_id)。 – Paystey 2011-11-16 08:50:04