我建立了以下关系:
老师:
var $hasAndBelongsToMany = array( 'Classroom' => array( 'className' => 'Classroom', 'joinTable' => 'classrooms_teachers', 'foreignKey' => 'teacher_id', 'associationForeignKey' => 'classroom_id', 'unique' => true, ) );课堂:
var $hasAndBelongsToMany = array( 'Teacher' => array( 'className' => 'Teacher', 'joinTable' => 'classrooms_teachers', 'foreignKey' => 'classroom_id', 'associationForeignKey' => 'teacher_id', 'unique' => true, ) ); var $hasMany = array( 'Student' => array( 'className' => 'Student', 'foreignKey' => 'classroom_id', 'dependent' => false, ), );学生:
var $belongsTo = array( 'Classroom' => array( 'className' => 'Classroom', 'foreignKey' => 'classroom_id', ), );我正在尝试为教师创建一个仪表板,其中显示通过教室与教师相关的所有学生。
我正在使用以下查找操作:
$students = $this->Teacher->Classroom->find('all', array( 'conditions' => array('Classroom.teacher_id' => $this->Access->getTeacherId()), ));但是,我收到一个错误: Unknown column 'Classroom.teacher_id' in 'where clause'
我必须做错事,因为蛋糕没有做关联。
有任何想法吗?
I have the following relationships set up:
Teacher:
var $hasAndBelongsToMany = array( 'Classroom' => array( 'className' => 'Classroom', 'joinTable' => 'classrooms_teachers', 'foreignKey' => 'teacher_id', 'associationForeignKey' => 'classroom_id', 'unique' => true, ) );Classroom:
var $hasAndBelongsToMany = array( 'Teacher' => array( 'className' => 'Teacher', 'joinTable' => 'classrooms_teachers', 'foreignKey' => 'classroom_id', 'associationForeignKey' => 'teacher_id', 'unique' => true, ) ); var $hasMany = array( 'Student' => array( 'className' => 'Student', 'foreignKey' => 'classroom_id', 'dependent' => false, ), );Student:
var $belongsTo = array( 'Classroom' => array( 'className' => 'Classroom', 'foreignKey' => 'classroom_id', ), );I am trying to create a dashboard for the teachers, where all students associated with the teacher through the classroom are displayed.
I am using the following find operation:
$students = $this->Teacher->Classroom->find('all', array( 'conditions' => array('Classroom.teacher_id' => $this->Access->getTeacherId()), ));However, I am getting an error: Unknown column 'Classroom.teacher_id' in 'where clause'
I must be doing something wrong because cake is not doing the associations.
Any ideas?
最满意答案
那不是最好的方式。 只需使用containsable和find('first'),你不需要找到所有:
$students = $this->Teacher->find('first', array( 'conditions' => array('Teacher.id' => $this->Access->getTeacherId()), 'contain' => array('Classroom'=>array('Student')) ));
这将为您提供与教师相关的所有教室以及这些教室中的所有学生。
That's not the best way. Just use containable and find('first'), you don't need find all:
$students = $this->Teacher->find('first', array( 'conditions' => array('Teacher.id' => $this->Access->getTeacherId()), 'contain' => array('Classroom'=>array('Student')) ));
That will get you all the classrooms associated with the teacher, and all students in those classrooms.
更多推荐
发布评论