我将使用什么查询

时间:2011-04-13 21:54:00

标签: php database orm doctrine propel

我有一个Relationship表,将教师与学生联系起来

Teacher   Student
1         1
1         2
2         1 
2         3

Students表格中有关于学生的具体数据

Id    Date of birth   First Name
1     1990-10-10      Bill Smith
2     1989-01-03      Adam Smithson

我想选择所有的老师学生1.现在我这样做(这是推动ORM语法)。

$relationships = RelationshipQuery::create()->filterByTeacher($teacherid)->find();

foreach($relationships as $relationship){
  $studentId = $relationship->getStudent();

  //use the studentId to get the actual student object
  $student = StudentQuery::create()->findPk($studentId);

  //after I get the student, I add it to an array
  $students[] = $student;
}

这个问题是我最终得到一个数组,而不是我们在做正常->find()时最终得到的通常的propelCollection。有没有办法清理这个查询(使用连接或类似的东西),以便我从一开始就得到一个PropelCollection?

1 个答案:

答案 0 :(得分:0)

您应该为关系定义架构,如下所示:http://www.propelorm.org/wiki/Documentation/1.5/Relationships#Many-to-ManyRelationships

您要求的代码非常简单:

$teacher = TeacherQuery::create()->findOneById($teacherId);
$students = $teacher->getStudents(); //Your students collection