我会为此使用什么查询
我有一个将教师与学生联系起来的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该像这样定义关系的架构: http:// www.propelorm.org/wiki/Documentation/1.5/Relationships#Many-to-ManyRelationships
您要求的代码非常简单:
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: