开发者

What query would I use for this

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 s开发者_如何学Cyntax).

$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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜