连接两个以上的表 cakephp

发布于 2024-12-20 10:36:47 字数 402 浏览 0 评论 0原文

我正在编写一个显示住房单元的应用程序(它们由提供出租的用户列出)。我试图弄清楚如何在 Cakephp 中定义我的单位模型。我有三个表:单位、复合体和用户。

我知道单元 $ 属于“用户”,但它也属于“复杂”,因为每个复杂可以有许多单元。我可以写这个吗?

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

} ?>

我还应该补充一点,我不确定如何为用户和复杂定义我的类,因为用户可以有许多复杂,而复杂可以有许多用户。如果一个人有很多,它也可以属于它有很多的东西吗?这是 HABTM 吗?我很困惑(新手)。

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. 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. 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. If one hasMany can it also belongTo the thing that it hasMany of? Is this a HABTM? I am very confused (newbie).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌伤浅笑 2024-12-27 10:36:47

听起来好像一个用户有很多情结,一个情结又属于很多用户。
这是一个 HABTM。

一个用户可以有多个单位,但一个单位只能有一个用户。这是一个hasMany 和一个belongsTo。

现在对于单位和复合体之间的关系,如果一个复合体可以有多个单位,那么那就是一个 hasMany。我猜是这样的。我还猜测,一个单元既属于一个综合体,也属于一个用户。这很好。单元可以同时属于用户和综合体,没有问题或不需要 HABTM。

所以我在上面已经为你定义了你的关系。看来您已经知道如何定义 hasMany 和 ownTo 了,因此只需确保按照本页所述设置 HABTM 即可:(请记住,您的数据库中需要一个额外的表,并且不要忘记添加任何数据库其他关系也需要您的列)。

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

祝你好运。

Sounds like a user has many complexes and a complex belongs to many users.
This is a HABTM.

A user can have many units but a unit can only have one user. This is a hasMany and a belongsTo.

Now for the relationship between units and complexes, if a complex can have many units, then that is a 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.

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).

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

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文