2013-07-14 36 views
0

我有這個表:Yii中得到很多很多的橋表中獲取所有列

Careeer { id(pk) , name} 
Student { email (pk), ...} 
Career_Student { student_email(pk)(fk), career_id (pk)(fk), semester } 

在我的Yii模型我有這個關係

'careers' => array(self::MANY_MANY, 'Career', 'Career_Student(student_email, career_id)') 

最後我有這個疑問

$criteria = new CDbCriteria(); 
$params = array (':email' => $_SESSION['USER']); 
$criteria->addCondition("email=:email"); 
$criteria->params = $params; 
$result = Student::model()->find($criteria); 
$careers=$result->careers; 

我的問題是,Yii沒有檢索我的$ careers變量中的「學期」列。

¿我該怎麼做?

預先感謝您!

回答

0

您可以表達MANY_MANY關係作爲與through選項連接的兩個HAS_MANY關係的組合:

'career_student' => array(self::HAS_MANY,'Carrer_Student','student_email'), 
'careers' => array(self::HAS_MANY,'Career', 'career_id','through'=>'career_student'), 

還要確保,您在Career_Student模型定義career的關係:

'career' => array(self::BELONGS_TO, 'Career', 'career_id'), 

現在,當您查詢學生with('careers'),這兩個關係將被填充:

$criteria->with = 'carreers'; 
$result = Student::model()->findAll($criteria); 
foreach($student->career_student as $careerStudent) { 
    // You have access to the bridge table ... 
    echo $careerStudent->semester; 

    // ... and the connected career table 
    echo $careerStudent->career->name; 
} 
+0

謝謝先生!這工作很不錯:) –