CakePHP hasAndBelongsToMany vs hasMany通过 [英] CakePHP hasAndBelongsToMany vs hasMany through

查看:156
本文介绍了CakePHP hasAndBelongsToMany vs hasMany通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是我的第3个CakePHP应用程序,第一个要求HABTM类型关联。我正在使用Cake 2.1,其中有一个更改HABTM参数允许通过将'unique'键设置为keepExisting来保存额外的信息。在一个正常的HABTM协会,你有一个表有2个字段,每个外键。我想我需要一个有3个外键。可以通过使用'unique'键,还是应该通过关联使用hasMany?

This is only my 3rd CakePHP app, and the first to require a HABTM type association. I'm working with Cake 2.1, in which there was a change to HABTM parameters allowing for extra information to be saved by setting the 'unique' key to 'keepExisting'. In a normal HABTM association you have a table with 2 fields, each foreign keys. I think I need one with 3 foreign keys. Is that possible by using the 'unique' key, or should I use a hasMany through association?

这是我的模式:

/* repair_orders */
CREATE TABLE repair_orders (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    number VARCHAR(16),  //Auto-generated repair order number 
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);

/* Employees */
CREATE TABLE employees (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(16), 
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);

/* op_codes */
CREATE TABLE op_codes (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(16),
    sale_material DECIMAL(10,2),
    cost_material DECIMAL(10,2), 
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);

操作代码本质上是一个项目及其成本的列表。

The op codes are a essentially a list of items and their cost.

修复订单可以有多个操作代码。我还需要跟踪哪个员工被分配到每个维修订单上的特定操作代码。所以我认为需要另一个表,如:

A repair order can have many op codes. I also need to keep track of which employee is assigned to the specific op codes on each repair order. So I think that requires another table, something like:

/* repair_order_assignments */
CREATE TABLE repair_order_assignments (
    repair_order_id INT(16),
    op_code_id INT(16),
    employee_id INT(16)
);

所以我的协会是:

Repair_Order_Assignment hasMany Employee, Op_Code
Employee belongsTo Repair_Order_Assignment
Op_Code belongsTo Repair_Order_Assignment

Repair_Order_Assignment hasAndBelongsToMany Repair_Order
Repair_Order hasAndBelongsToMany Repair_Order_Assignment

根据手册,当保存hasAndBelongsToMany关联时,将首先删除关联。您将丢失列中的额外数据,因为它不会在新插入中替换。然而,它注意到在2.1有一个唯一的变量,可以设置为保存额外的数据。这个设置是否与唯一键一起使用,还是应该使用hasMany通过方法?

According to the manual, when hasAndBelongsToMany associations are saved, the association is deleted first. You lose the extra data in the columns as it is not replaced in the new insert. However, it goes on to note that in 2.1 there is a unique variable that can be set to save the extra data. Will this setup work with the unique key or should I use a hasMany through approach?

推荐答案

一个HABTM关系是标签和多个类别。

I've found the only thing that a HABTM relationship for is tags and multiple categories.

我使用的任何类型的会员资格,友谊或关系关系都有很多通过,因为有时候很好的附加额外的数据到关系,即使它只是创建/修改。另一件让我离开HABTM的事情是替换 - 一切更新逻辑。导致很多麻烦,保存模型的不同部分。

Any sort of membership, friendship or following relationship I use has many through because sometimes it's nice to attach extra data to a relationship, even if it's just created/modified. Another thing that puts me off HABTM is the replace-everything-upon-update logic. Causes a lot of bother saving different parts of a model.

这篇关于CakePHP hasAndBelongsToMany vs hasMany通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆