简体   繁体   English

加入两个以上的表cakephp

[英]join more than two tables cakephp

I am writing an app that displays housing units (they are listed by users offering them for rent). 我正在编写一个显示房屋单位的应用程序(这些房屋按用户列出来出租)。 I am trying to figure out how to define my Unit model in Cakephp. 我试图弄清楚如何在Cakephp中定义我的单元模型。 I have three tables: units, complexes and users. 我有三个表:单位,组合物和用户。

I know that unit $belongsTo 'User', but it also belongs to 'Complex', since every complex can have many units. 我知道单位$ belongsTo为“用户”,但它也属于“复合物”,因为每个复合物可以有许多单位。 Can my I write this? 我可以写这个吗?

<?php
  class Unit extends AppModel {
        var $name='Unit';
        var $belongsTo=array('User', 'Complex');

} ?> }?>

I should also add that I am not sure how to define my classes for User and Complex, as a User can have many Complexes and a Complex can have many Users. 我还应该补充一点,我不确定如何为User和Complex定义类,因为User可以有很多Complex,而Complex可以有很多Users。 If one hasMany can it also belongTo the thing that it hasMany of? 如果一个人有很多也可以属于它有很多的东西? Is this a HABTM? 这是HABTM吗? I am very confused (newbie). 我很困惑(新手)。

Sounds like a user has many complexes and a complex belongs to many users. 听起来一个用户有很多复合体,而一个复合体属于许多用户。 This is a HABTM. 这是HABTM。

A user can have many units but a unit can only have one user. 一个用户可以有多个单位,但一个单位只能有一个用户。 This is a hasMany and a belongsTo. 这是一个hasMany和一个belongsTo。

Now for the relationship between units and complexes, if a complex can have many units, then that is a hasMany. 现在,对于单元和复合体之间的关系,如果一个复合体可以具有多个单元,则这就是hasMany。 I would guess that to be the case. 我想是这样。 I would also guess that a unit belongs to a complex as well as a user. 我还要猜测一个单元既属于用户,也属于用户。 This is fine. 这可以。 Units can belong to both users and complexes without issue or needing a HABTM. 单元既可以属于用户,也可以属于联合体,而不会出现问题或需要HABTM。

So I hav defined your relationships for you above. 因此,我在上面为您定义了您的关系。 It seems like you know how to define hasMany and belongsTo already, so just make sure you set up HABTM as described on this page: (keep in mind that you need an extra table in your database, and don't forget to add any db columns you need for the other relationships, either). 似乎您已经知道如何定义hasMany和belongsTo,因此请确保按照页面上的说明设置HABTM :(请记住,数据库中需要一个额外的表,并且不要忘记添加任何数据库其他关系所需的列)。

http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM

Good luck. 祝好运。

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

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