我有三个模型:
// student_id student_name
Student extends ActiveRecord\Model
{
static $has_many = array(
array('studentcourses')
);
}
// course_id course_name
Course extends ActiveRecord\Model
{
static $has_many = array(
array('students')
);
}
// course_id student_id
Studentcourse extends ActiveRecord\Model
{
static $belongs_to = array(
array('student')
);
}
因此,通过这样做,我将能够轻松地从这些表中检索数据:
$student = Student::find(1);
foreach ($student->studentcourses as $course)
{
echo $course->name;
}
但是我不想输入“studentcourses”,我想输入 $student->courses 来获取学生参加的所有课程,并输入 $course->students 来获取特定课程的学生列表。关系设置将如何实现?