【问题标题】:What query would I use for this我会为此使用什么查询
【发布时间】:2011-04-13 21:53:59
【问题描述】:

我有一个 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?

【问题讨论】:

    标签: php database orm doctrine propel


    【解决方案1】:

    您应该为这样的关系定义架构:http://www.propelorm.org/wiki/Documentation/1.5/Relationships#Many-to-ManyRelationships

    你要的代码很简单:

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

    【讨论】:

      猜你喜欢
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多