问题描述
我试图发现到处都是这样,但我不能。这里是我的问题:
I tried to find this everywhere but I cant. Here is my problem:
可以说我有这样的相关机型:
Lets say I have models associated like this:
- 学生(有许多成员)
- 会员(属于学生和TeacherCourse)
- TeacherCourse (属于教师和课程)
- 教师(有很多TeacherCourses)
- 课程(有很多TeacherCourses)
- Student (has many Memberships)
- Membership (belongs to Student and TeacherCourse)
- TeacherCourse (belongs to Teacher and Course)
- Teacher (has many TeacherCourses)
- Course (has many TeacherCourses)
当我使用 membershipsController
所有成员发送到视图,我只得到例如 $会员['会员'] ['ID' ]
或 $会员['TeacherCourse'] ['身份证']
BUT NOT $会员['TeacherCourse'] [ '老师'] ['身份证']
...
When I use membershipsController
to send all membership to the view, I get only for example $membership['Membership']['id']
or $membership['TeacherCourse']['id']
BUT NOT $membership['TeacherCourse']['Teacher']['id']
...
这意味着我得到的信息会员资格和TeacherCourse,因为他们是直接相关的。我的问题是我怎么也可以获取有关教师直接或课程的信息?
Which means I get info on Memberships and TeacherCourse because they are directly associated. My question is how can I also get info about Teachers or Courses directly?
推荐答案
您可以增加模型的递归级别,以获得更深层次的关联(设置 $这个 - >成员─>递归
2或3),但一般最好还是使用的所以你可以选择你想要检索的关联。例如,这将返回成员关联TeacherCourse和教师模式:
You could increase the model's recursion level to obtain deeper associations (set $this->Membership->recursive
to 2 or 3), but in general it's better to use the Containable
behavior so you can choose which associations you want to retrieve. For example this would return memberships with associated TeacherCourse and Teacher models:
$membership = $this->Membership->find('all', array(
'contain'=>array(
'TeacherCourse'=>array(
'Teacher'=>array()
),
));
又见容纳的行为的完整文档在
这篇关于CakePHP的模型协会(检索数据更深)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!