Cakephp 关系

发布于 2024-09-29 14:58:21 字数 1026 浏览 0 评论 0原文

我有一些模型不是默认的 cakephp 格式

user(id, name...)

1 Harsha ...
2 John ....

dishes(id, name, Price ...)

1 "Cheese Pizza" 6
2 "Zinger Burger" 3

餐厅(id,名称,......)

1 "KFC" ...
2 "Pizza Hut" ...

模块(id,名称)值(用户,菜肴,餐厅)

1 "Users"
2 "Dishes"
3 "Restaurant"

项目(id,module_id,item_id)< /strong>

1 1 1 (refers to User Harsha)

2 3 2 (refers to Pizza hut)

3 2 2 (refers to Zinger Burger)

4 1 2 (refers to User John)

其中 item_id 指的是用户、菜肴或休息的 ID,具体取决于 module_id


评论(id、parent_id、评论、时间、item_id、commenter_id)

1 0 "Best Burger in the world" "time" 3 1 (refers to Harsha reviewing Zinger Burger)

2 1 "Yes i love Zingers tooo" time 3 2 ( refers to John replying to Harsha's Review)

我对如何绘制有点混乱建立cakephp中的关系

I have a few models which are not in the default cakephp format

user(id, name....)

1 Harsha ...
2 John ....

dishes(id, name, price ...)

1 "Cheese Pizza" 6
2 "Zinger Burger" 3

restaurants (id, name, .....)

1 "KFC" ...
2 "Pizza Hut" ...

module(id, name) values (User, Dishes, Restaurants)

1 "Users"
2 "Dishes"
3 "Restaurant"

items (id, module_id, item_id)

1 1 1 (refers to User Harsha)

2 3 2 (refers to Pizza hut)

3 2 2 (refers to Zinger Burger)

4 1 2 (refers to User John)

where item_id refers to the Id of Users, Dishes or Rests Depending on the module_id


reviews (id, parent_id, review, time, item_id, commenter_id)

1 0 "Best Burger in the world" "time" 3 1 (refers to Harsha reviewing Zinger Burger)

2 1 "Yes i love Zingers tooo" time 3 2 ( refers to John replying to Harsha's Review)

I am a little messged up on how to draw up the relationships in cakephp

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

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

发布评论

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

评论(1

像你 2024-10-06 14:58:21

在此页面的书中: http://book.cakephp.org/ view/1039/Associations-Linking-Models-Together 您将找到可以在关系上设置的可能键的指南,例如

foreignKey:外键名称
在另一个模型中找到钥匙。这是
如果您需要定义的话特别方便
多个 hasOne 关系。这
该键的默认值是
带下划线的单数名称
当前型号,后缀为“_id”。在
上面的例子默认为
“user_id”。

假设评论和项目是其关联中的子项,对于关系的两端,您可以将foreignKey 设置为“item_id”。

例如:

dishes:
class Dish extends AppModel {
    var $name = 'Dish'; 
    var $hasMany = array(
        'Item' => array(
            'className'     => 'Item',
            'foreignKey'    => 'item_id',
            ...
items:
class Item extends AppModel {
    var $name = 'Item'; 
    var $belongsTo = array(
        'Dish' => array(
            'className'     => 'Dish',
            'foreignKey'    => 'item_id',
            ...
            ),

        'Restaurant' => array(
            'className'     => 'Restaurant',
            'foreignKey'    => 'item_id',
            ...

很难更精确,但您还没有提供任何数据模型。要通过模块处理模型的选择,您需要在模型的某个位置编写一些代码,具体代码取决于您访问数据的方式。

然而,在我看来,现在是重组数据库的好时机!

In the book at this page: http://book.cakephp.org/view/1039/Associations-Linking-Models-Together you'll find a guide to the possible keys you can set on the relationship, e.g.

foreignKey: the name of the foreign
key found in the other model. This is
especially handy if you need to define
multiple hasOne relationships. The
default value for this key is the
underscored, singular name of the
current model, suffixed with ‘_id’. In
the example above it would default to
'user_id'.

Assuming reviews and items are children in their associations, for both ends of the relationships, you'd set the foreignKey as 'item_id'.

Something like:

dishes:
class Dish extends AppModel {
    var $name = 'Dish'; 
    var $hasMany = array(
        'Item' => array(
            'className'     => 'Item',
            'foreignKey'    => 'item_id',
            ...
items:
class Item extends AppModel {
    var $name = 'Item'; 
    var $belongsTo = array(
        'Dish' => array(
            'className'     => 'Dish',
            'foreignKey'    => 'item_id',
            ...
            ),

        'Restaurant' => array(
            'className'     => 'Restaurant',
            'foreignKey'    => 'item_id',
            ...

It's difficult to be more precise, but you haven't provided any datamodel. To handle the selection of Model via the Module, you'll need to write some code on a model somewhere, which one(s) depends on how you access the data.

However, it looks to me like a good time to restructure the database!

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