cakePHP HABTM找到条件(cakePHP HABTM find with conditions)

编程入门 行业动态 更新时间:2024-10-26 22:23:31
cakePHP HABTM找到条件(cakePHP HABTM find with conditions)

我建立了以下关系:

老师:

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.

更多推荐

本文发布于:2023-08-07 21:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   HABTM   cakePHP   find   conditions

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!