我会为此使用什么查询

发布于 2024-11-01 03:40:24 字数 913 浏览 4 评论 0原文

我有一个将教师与学生联系起来的 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 的学生的所有学生。我这样做(这是 propel 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?

I have a Relationship table that links teachers to students

Teacher   Student
1         1
1         2
2         1 
2         3

The Students table then has specific data about the students

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

I want to select all the students who are students of teacher 1. Right now i do this way (this is propel ORM syntax).

$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;
}

The problem with this is that I end up with an array, and not the usual propelCollection that we end up with when we do a normal ->find(). Is there a way to clean up this query a bit (use joins or something like that) so that I end up with a PropelCollection from the start?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就像说晚安 2024-11-08 03:40:24

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

您要求的代码非常简单:

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

You should define your schema for the relationship like this: http://www.propelorm.org/wiki/Documentation/1.5/Relationships#Many-to-ManyRelationships

The code you're asking for is very easy:

$teacher = TeacherQuery::create()->findOneById($teacherId);
$students = $teacher->getStudents(); //Your students collection
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文