【问题标题】:Symfony 1.4/ Doctrine; n-m relation data cannot be accessed in template (indexSuccess)Symfony 1.4/教义;无法在模板中访问 n-m 关系数据(indexSuccess)
【发布时间】: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


    【解决方案1】:

    问题在于您没有定义 n-m 关系,而是定义了两个 1-n 关系。从数据库的角度来看,这是相当等价的,但从应用程序的角度来看,您将获得 4 个没有太多语义价值的方法,而您可以获得两个易于使用的方法:Student::getCourses() 和 @987654323 @

    阅读this § of the doctrine1.2 documentation 了解如何实现这一目标。 StudentHasCourse 应该用作您的refClass

    更新 我刚刚了解到您将结果存储在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>
    

    【讨论】:

    • 非常感谢您提供的信息 greg0ire。这将帮助我解决问题。我会检查并通知您。
    • 我照你说的做了。我可以创建访问方法getCourses()getStudents()。给定student id,我可以学习课程。然而,我无法得到StudentHasCourse 类的结果。虽然在上面的类中有一个 getResult() 我没有找到一种方法来获得它。我最终需要的是这样的结果; course_id, course_name, result(来自关联表)给出了student_id。如果可能的话,如果您能详细说明您的答案以实现这一目标,我们将不胜感激。非常感谢您的宝贵时间。
    • 再次感谢您的支持。我会检查你的方法并通知你。
    • 感谢@greg0ire 的帮助。但问题依然存在;尝试访问 getResult() Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'Doctrine_Collection' does not have a method 'getResult' in D:\wamp\bin\php\php5.3.5\PEAR\pear\symfony\escaper\sfOutputEscaperObjectDecorator.class.php on line 64 时会给出上述警告。如果您已完成上述操作并获得了正确的结果,那将是我这边的一个问题。如果是这样,请帮我找出来。否则我必须进一步调查。
    • 当然我没有测试我上面写的任何东西:我不会只为你建立一个测试项目和一个数据库,是吗?我会犯错误。例如,这里的请求应该将studentHasCourse 限制为一个元素,studentHasCourse is still 是一个集合,您应该首先在其上使用 get。这样做并检查该集合是否只有一个元素,我不是 100% 确定。我更新了我的答案,我认为是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多