2011-11-27 78 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

相关问题