Symfony 1.4 /学说; n-m关系数据无法在模板(indexSuccess)中访问 [英] Symfony 1.4/ Doctrine; n-m relation data cannot be accessed in template (indexSuccess)

查看:110
本文介绍了Symfony 1.4 /学说; n-m关系数据无法在模板(indexSuccess)中访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有3个表的数据库。这是一个简单的n-m关系。学生,课程和学生课程处理n-m关系。我发布schema.yml以供参考,但实际上并不是必须的。

 课程:
连接:doctrine
tableName:课程
列:
id:
type:integer(4)
fixed:false
unsigned:false
primary:true
autoincrement:false
name:
类型:string(45)
fixed:false
unsigned:false
primary:false
notnull:false
autoincrement:false
关系:
StudentHasCourse:
local:id
foreign:course_id
type:many

学生:
连接:doctrine
tableName:student
列:
id:
类型: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
名称:
类型:string(30)
fixed:false
unsigned:false
primary:false
notnull:false
autoincrement:false
关系:
StudentHasCourse:
local:id
foreign:student_id
type:many

StudentHasCourse:
connection:doctrine
tableName:student_has_course
列:
student_id:
类型:integer(4)
fixed:false
unsigned:false
primary:true
autoincrement:false
course_id:
type:integer(4)
fixed:false
unsigned:false
primary:true
autoincrement:false
result:
类型:string(1)
fixed:true
unsigned:false
primary:false
notnull:false
autoincrement:false
关系:
课程:
local:course_id
foreign:id
type:one
Stu凹痕:
本地:student_id
外国人:id
类型:一个

然后,我从以下查询中的executeIndex()中的表中获取数据。

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

然后我通过在indexSuccess.php中循环访问数据。但是,在indexSuccess中,我只能从表中访问数据。

 <?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()?>

第一个语句发出警告;


警告:call_user_func_array()期望参数1是一个有效的回调,类Doctrine_Collection在D:\wamp\bin\php\php5中没有方法'getCourse'。第64行


第二条语句给出以上警告和以下错误;


致命错误:调用D中的非对象的成员函数getName():第5行上的\wamp\www\sam\test_doc_1\apps\frontend\modules\registration\templates\indexSuccess.php


当我从Debug工具栏检查查询时,显示如下,它显示了我想要的所有数据。

  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课程c ON s2.course_id = c.id
WHERE(c.id = 1)

虽然问题很短,但所提到的所有信息都变得如此之长。非常感谢有人可以帮我解决这个问题。我要求的是访问StudentHasCourse和Course的数据。如果这个设计和这个查询无法访问这些数据,任何其他的方法也是值得赞赏的。

解决方案

问题在于事实上,您没有定义nm关系,而是两个1-n关系。这从数据库的角度来看是相当的,但是从应用程序的角度来看,你可以得到4种方法,没有太多的语义值,当你可以得到两个易于使用的方法:学生:: getCourses() Course :: getStudents()



阅读<一个href =http://www.doctrine-project.org/projects/orm/1.2/docs/manual/defining-models/ru#relationships%3ajoin-table-associations%3amany-to-many =nofollow >这个§的doctrine1.2文档学习如何实现这一点。 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 //在课程中实现__toString?> :<?php echo $ course-> getStudentHasCourse() - > getFirst() - > getResult(); ?>< /锂>
<?php endforeach;?>
< / ul>


I have a database with 3 tables. It's a simple n-m relationship. Student, Course and StudentHasCourse to handle n-m relationship. I post the schema.yml for reference, but it would not be really necessary.

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

Then, I get data from tables in executeIndex() from the following query.

  $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();

Then I access data by looping through in indexSuccess.php. But, in indexSuccess I can only access data from the table Student.

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

I expected, that I could access StudentHasCourse data and Course data like the following. But, it generates an error.

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

The first statement gives a warning;

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

And the second statement gives the above warning and the following error;

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

When I check the query from the Debug toolbar it appears as following and it gives all data I want.

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)

Though the question is short, as all the information mentioned it became so long. It's highly appreciated if someone can help me out to solve this. What I require is to access the data from StudentHasCourse and Course. If those data cannot be accessed by this design and this query, any other methodology is also appreciated.

解决方案

The problem lies in the fact that you didn't define an n-m relationship, but two 1-n relationships. This is quite equivalent from a database point of view, but from the application point of view, you are getting 4 methods that do not have much semantic value, when you could get two easy-to-use methods : Student::getCourses() and Course::getStudents()

Read this § of the doctrine1.2 documentation to learn how to achieve this. StudentHasCourse should be used as your refClass

UPDATE I just understood you were storing the result in StudentHasCourse To get what you want, try something like this in your controller:

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

This means you still need the StudentHasCourse relation in the Course class. Recover it if you deleted it.

Now you should be able to do something like this in your template

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

这篇关于Symfony 1.4 /学说; n-m关系数据无法在模板(indexSuccess)中访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆