【发布时间】:2015-07-27 01:54:20
【问题描述】:
假设我的设置与 CookBook 中的设置完全相同: http://book.cakephp.org/3.0/en/orm/associations.html
class StudentsTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Courses', [
'through' => 'CourseMemberships',
]);
}
}
class CoursesTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Students', [
'through' => 'CourseMemberships',
]);
}
}
class CoursesMembershipsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Students');
$this->belongsTo('Courses');
}
}
Student BelongsToMany Course
Course BelongsToMany Student
id | student_id | course_id | days_attended | grade
我应该如何构造查询来查找给定学生的课程,他的成绩 ==“A”?
$query = $this->Courses->find('all')
->contain(['CourseMemberships'])
->where(['CourseMemberships.student_id' => $student['id'], 'CourseMemberships.grade' => 'A']);
这行不通。应该怎么写?
【问题讨论】:
-
此代码触发错误:“Courses is not associated with CourseMembers”
标签: cakephp cakephp-3.0