简体   繁体   English

cakephp在一个联接表中一次联接两个以上的表

[英]cakephp join more than 2 tables at once in one join table

I have converted many of my projects across to Cake with success, but I have one that poses a challenge that I am struggling with. 我已经成功地将我的许多项目转换为Cake,但我遇到的一个项目却给我带来了挑战。

In a typical " hasMany through " join, eg. 在典型的“ hasMany through ”连接中,例如。 in the Cake docs, you have 3 tables, Student , Course and CourseMembership . 在Cake文档中,您有3个表, StudentCourseCourseMembership Student and Course hasMany CourseMembership and CourseMembership belongsTo Student and Course . StudentCourse hasMany CourseMembershipCourse hasMany CourseMembership CourseMembership belongsTo StudentCourse

My question is, is it possible, using normal convention, to join more than two tables together in this way? 我的问题是,是否可以使用常规约定以这种方式将两个以上的表连接在一起? For example, adding another table, say, Lecturer (forget the real world possibilities of this) would create more possible permutations but can Cake handle this natively? 例如,添加另一个表,例如Lecturer (忘记现实的可能性)会创建更多可能的排列,但是Cake可以本地处理吗?

Thanks in advance. 提前致谢。

EDIT: here would be the table layout for the join (HTH): 编辑:这将是联接(HTH)的表布局:

id | student_id | course_id | lecturer_id

EDIT 2: by default, the join table would be called course_students so what I am after is a table called course_lecturer_students with the above layout (obviously it doesn't need to be called that but just to comply with CakePHP naming convention). 编辑2:默认情况下, course_students表将被称为course_students所以我需要的是具有上述布局的名为course_lecturer_students的表(显然,不需要调用它,而只是为了符合CakePHP命名约定)。

Absolutely. 绝对。 You can Join as many tables as you'd like really. 您可以根据需要Join任意多个表。 Using CakePHP to Join is no different than using MySQL to run a Join query - CakePHP just takes the CakePHP syntax and creates the MySQL (or whatever) query for you. 使用CakePHP进行联接与使用MySQL运行联接查询没有什么不同-CakePHP只是采用CakePHP语法并为您创建MySQL(或其他)查​​询。

Side note - Just make sure you're indexing your tables correctly. 旁注-只需确保您正确索引了表即可。

You could use Cakephp for 您可以将Cakephp用于

  1. Write simple sql as query ( no recomendable ) 将简单的sql编写为查询(不可推荐)
  2. Write join sentences 写连接句子
  3. use deep associations ( recomendable ) 使用深层关联(可推荐)

However you question is not clear and is not possible to determinate which kind of answer you are looking for, or what are you looking for 但是,您的问题尚不清楚,无法确定您正在寻找哪种答案,或者您正在寻找什么?

Probably you could extends with a final result of what you are looking for in order to help you 可能您可以扩展所需的最终结果以帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM