2011-11-27 41 views
2

我有一個包含3個表的數據庫。這是一個簡單的n-m關係。學生,課程和StudentHasCourse處理n-m關係。我張貼schema.yml作爲參考,但它不是真的有必要。Symfony 1.4/Doctrine; n-m關係數據無法在模板中訪問(indexSuccess)

Course: 
    connection: doctrine 
    tableName: course 
    columns: 
    id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: false 
    name: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    relations: 
    StudentHasCourse: 
     local: id 
     foreign: course_id 
     type: many 

Student: 
    connection: doctrine 
    tableName: student 
    columns: 
    id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: false 
    registration_details: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    name: 
     type: string(30) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    relations: 
    StudentHasCourse: 
     local: id 
     foreign: student_id 
     type: many 

StudentHasCourse: 
    connection: doctrine 
    tableName: student_has_course 
    columns: 
    student_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: false 
    course_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: false 
    result: 
     type: string(1) 
     fixed: true 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    relations: 
    Course: 
     local: course_id 
     foreign: id 
     type: one 
    Student: 
     local: student_id 
     foreign: id 
     type: one 

然後,我從以下查詢中獲取來自executeIndex()中表的數據。

$q_info = Doctrine_Query::create() 
    ->select('s.*, shc.*, c.*') 
    ->from('Student s') 
    ->leftJoin('s.StudentHasCourse shc') 
    ->leftJoin('shc.Course c') 
    ->where('c.id = 1'); 
    $this->infos = $q_info->execute(); 

然後,我通過在indexSuccess.php的循環通過訪問數據。但是,在indexSuccess中,我只能訪問Student表中的數據。

<?php foreach ($infos as $info): ?> 
    <?php echo $info->getId(); ?> 
    <?php echo $info->getName(); ?> 
<?php endforeach; ?> 

我想,我可以訪問StudentHasCourse數據和課程數據,如下所示。 但是,它會產生一個錯誤。

<?php echo $info->getStudentHasCourse()->getResult()?> 
<?php echo $info->getStudentHasCourse()->getCourse()->getName()?> 

第一條語句給出警告;

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'Doctrine_Collection' does not have a method 'getCourse' in D:\wamp\bin\php\php5.3.5\PEAR\pear\symfony\escaper\sfOutputEscaperObjectDecorator.class.php on line 64

而第二個聲明給出了上述警告和以下錯誤;

Fatal error: Call to a member function getName() on a non-object in D:\wamp\www\sam\test_doc_1\apps\frontend\modules\registration\templates\indexSuccess.php on line 5

當我檢查從它出現如下調試工具欄查詢和它給我想要的所有數據。

SELECT s.id AS s__id, s.registration_details AS s__registration_details, s.name AS s__name, s2.student_id AS s2__student_id, s2.course_id AS s2__course_id, s2.result AS s2__result, c.id AS c__id, c.name AS c__name 
FROM student s LEFT JOIN student_has_course s2 ON s.id = s2.student_id LEFT JOIN course c ON s2.course_id = c.id 
WHERE (c.id = 1) 

雖然問題很簡短,但所提到的所有信息都變得如此之久。如果有人能幫助我解決這個問題,我將非常感激。我需要的是從StudentHasCourse和Course訪問數據。如果這些數據不能被這個設計和這個查詢訪問,那麼其他的方法也是值得讚賞的。

回答

1

問題在於你沒有定義一個n-m關係,而是兩個1-n關係。從數據庫的角度來看,這非常相當,但從應用程序的角度來看,您會得到4種語義值不太重要的方法,但您可以獲得兩種簡單易用的方法:Student::getCourses()Course::getStudents()

閱讀this § of the doctrine1.2 documentation瞭解如何實現此目的。 StudentHasCourse應作爲你的refClass

UPDATE 我明白你把結果存儲在StudentHasCourse 爲了得到你想要的,嘗試這樣的事情在你的控制器是什麼:

$this->courses = CourseTable::getInstance() 
    ->createQuery('c') 
    ->innerJoin('c.StudentHasCourse shc') 
     ->where('shc.student_id = ?', $student_id) 
     ->execute(); 

這意味着你仍然需要Course課程中的StudentHasCourse關係。如果你刪除它,恢復它。

現在,你應該能夠做這樣的事情在你的模板

<ul> 
<?php foreach($courses as $course): ?> 
    <li><?php echo $course //implement __toString in Course ?> : <?php echo $course->getStudentHasCourse()->getFirst()->getResult(); ?></li> 
<?php endforeach;?> 
</ul> 
+0

非常感謝您對您的信息greg0ire。這會幫助我解決問題。我會檢查它並讓你知道。 – chandimak

+0

我照你所說的去做了。我可以創建訪問方法_getCourses()_和_getStudents()_。給定一個「學生證」,我可以接受課程。然而,我無法得到'StudentHasCourse'類的結果。雖然在上面的類中有一個_getResult()_,但我沒有找到解決方法。我最終要求的結果就是:給予'student_id'的'course_id,course_name,result'(來自關聯表)。非常感謝,如果你可以詳細說明你的答案,如果可能實現這一點。非常感謝您的寶貴時間。 – chandimak

+0

我更新了我的答案 – greg0ire

相關問題