【发布时间】:2023-04-06 10:37:01
【问题描述】:
我有一个包含 3 个表的数据库。这是一个简单的 n-m 关系。 Student、Course 和 StudentHasCourse 来处理 n-m 关系。我发布了 schema.yml 以供参考,但这并不是真正必要的。
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 数据和 Course 数据,如下所示。 但是,它会产生错误。
<?php echo $info->getStudentHasCourse()->getResult()?>
<?php echo $info->getStudentHasCourse()->getCourse()->getName()?>
第一条语句给出警告;
警告:call_user_func_array() 期望参数 1 是一个有效的回调,类 'Doctrine_Collection' 在 D:\wamp\bin\php\php5.3.5\PEAR\pear\symfony\escaper 中没有方法 'getCourse' \sfOutputEscaperObjectDecorator.class.php 第 64 行
第二个语句给出了上述警告和以下错误;
致命错误:在第 5 行对 D:\wamp\www\sam\test_doc_1\apps\frontend\modules\registration\templates\indexSuccess.php 中的非对象调用成员函数 getName()
当我从“调试”工具栏检查查询时,它显示如下,它提供了我想要的所有数据。
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 访问数据。如果此设计和此查询无法访问这些数据,也欢迎使用任何其他方法。
【问题讨论】:
标签: doctrine symfony-1.4 dql