views:

65

answers:

3

I'm putting together a system to track customer orders. Each order will have three addresses; a Main contact address, a billing address and a shipping address. I do not want to have columns in my orders table for the three addresses, I'd like to reference them from a separate table and have some way to enumerate the entry so I can determine if the addressing is main, shipping or billing. Does it make sense to create a column in the address table for AddressType and enumerate that or create another table - AddressTypes - that defines the address enumeration and link to that table?

I have found other questions that touch on this topic and that is where I've taken my model. The problem I'm having is taking that into the cakePHP convention. I've been struggling to internalize the direction BelongsTo relationships are formed - the way the documentation states feels backwards to me.

Any help would be appreciated,

Thanks!

A: 

You are spot on. You could do either, depending on how much you want to normalise your database. Personally for me, I'd go with an AddressType model, which gives you the flexibility to add and remove address types at will.

If you want it simpler, then I would just go with an ENUM() field in your Address model.

DavidYell
A: 

I prefer to think of hasOne, hasMany and hasAndBelongsToMany as the three real relation types.

The belongsTo relation is there simply to do the reverse of what the former two (hasOne/Many) do.

If you look at this diagram, you will notice the pairing of hasOne/belongsTo and hasMany/belongsTo.

Also, note that model that "belongsTo" is the one storing the foreign key (eg. address_type_id).

So in your case, since an AddressType hasMany Address (ie. you can have many Home addresses), then the Address belongsTo AddressType (ie. each address needs an address_type_id).

deizel
A: 

Table1: address_types (id, name, active)

Table2: customers (id, name, active, etc.)

Table3: addresses (id, address_type_id, customer_id, country, city, street, etc.)

This way customers can have as many addresses as you want. If you need to add a new address type you must not alter the customers or addresses table.

bancer
Is the active column to represent the address that should currently be in use?
Ryan
The optional 'active' column is boolean to represent the address type that is currently valid (non-obsolete). F.ex., if you do not want to use 'Work address' type anymore in your application but there are records corresponding to that address type that you do not want to delete from the database.
bancer