问题描述
所以我想昨天晚上的工作了这一点,并在兜圈子思考自己以后几次决定我需要一些帮助。我做了一个发布关于ID字段不填充在的问题,但它得到了太多长篇大论和混乱,我意识到问题可能是基于关联。
So I'm trying to work this out last night, and after thinking myself around in circles a few times decided I need some help. I did post a question about an ID field not being filled in but it got too long winded and confusing, and I realised the problem was probably based on the associations.
我正在一个客户数据库。对于大企业客户的关系变得有些复杂。我有3个型号,客户,CustomerAddress和CustomerContact。客户可以有多个地址,而且很多接触,但接触也都是基于在同一地址,一个地址有许多联系。
I'm working on a customer database. For larger business customers the relationships get a little complicated. I have 3 models, Customer, CustomerAddress and CustomerContact. A customer can have many addresses, and many contacts, but the contacts are also based at one address so an address has many contacts.
最终的结果我想实现的是能够搜索数据两种方式,例如
The end result I'm trying to achieve is to be able to search the data both ways, e.g.
Customer -> Address 1 -> Contact 1, Contact 2, Contact 3, etc....
Address 2 -> Contact ... ... ...
....
或者....
Customer -> Contact 1 -> Address
Contact 2 -> Address
....
我遇到的问题是,一个地址必须的hasMany
联系人和联系人必须 hasOne
地址,但这两个关系冲突,因为对方应该的belongsTo
键,事实并非如此。
The issue I'm having is that an Address must hasMany
contacts and a contact must hasOne
address, but these 2 relationships conflict because the other side should belongsTo
and it doesn't.
推荐答案
这样的巧合。刚刚得知这最后一周!
据我所看到的,这里有一个基本的关系布局
Such coincidence. Just learnt this last week!As far as I can see, here is a basic relation layout
客户的hasMany地址
地址客户的belongsTo
Customer hasMany AddressAddress belongsTo Customer
地址的hasMany联系
联系地址的belongsTo
Address hasMany ContactContact belongsTo Address
现在这包括你想实现第一种情况的基本模型。要搜索通过地址从客户联系。
Now this covers the basic model of the first case you want to achieve. To search for Contact from Customer via Address.
至于第二种情况。从对客户接触查询将强大的昂贵,因此,我的建议是
As for the second case. Querying from the contact towards the Customer would be mighty expensive so, my suggestion is
创建一个关系:
客户的hasMany地址和的建立在这个的顶部的模型。这种关系是通过所谓的hasMany,其中两个型号都具有的hasMany关系,而且有更多的信息,骑在关系之上。例如:
Customer hasMany Address and build a Model on top of this. This kind of relation is called hasMany through where two Models have a hasMany relation but have more information riding on top of the relation. For example:
集团hasAndBelongsToMany用户
用户hasAndBelongsToMany集团
Group hasAndBelongsToMany UserUser hasAndBelongsToMany Group
这需要一个新的表groups_users。此外,每个基团可以有多个管理。假设我们下面的所有2.x的约定。蛋糕不允许在此表中额外列。因此,如果这种关系group_users被视为一个模型。因为我们希望我们可以在其中添加尽可能多的额外列。
This requires a new table groups_users. Also, each group can have multiple admin. Assuming we are following all 2.x conventions. Cake doesn't allow extra columns in this table. So if this relation group_users is treated as a Model. We can add as much extra columns in it as we want.
关系变得
GroupUser阵列的belongsTo('用户','集团');
用户的hasMany GroupUser
集团的hasMany GroupUser
Relation becomesGroupUser belongsTo array('User', 'Group');User hasmany GroupUserGroup hasmany GroupUser
在您的情况下创建的关系:客户的hasMany地址到一个模型。让我们称之为是CustomerAddress。
In your case create the relation: Customer hasMany Address to a Model. Lets call is CustomerAddress.
然后CustomerAddress的hasMany接触和联系的belongsTo CustomerAddress。
Then CustomerAddress hasMany Contact and Contact belongsTo CustomerAddress.
希望这有助于!
这篇关于如何正确设置多个协会3款车型之间(CakePHP的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!