I am rewriting an old hand-crafted-app of mine into CakePHP 2.0, and I bump into this old, ever-repeating Model design dilemma.
This is the target design:
- Order has many OrderEntries
- OrderEntry has fields [amount, type]
- ProductEntry extends OrderEntry with fields [product_id, quantity, weight]
- ShipmentEntry extends OrderEntry with fields [total_weight, country_id, city, address, etc.]
- DiscountEntry extends OrderEntry with fields [discount_code, parent_order_id, active, etc.]
EDIT: Radically simplifying question in lieu of new knowledge
So basically, I need this model structure from Cake:
Order 1:m OrderEntry
OrderEntry 1:1 ProductEntry
OrderEntry 1:1 ShipmentEntry
OrderEntry 1:1 DiscountEntry
The last 3 associations should be non-identifying. To be more accurate, one OrderEntry record can have exactly one relation to one of three child models.
This could be achieved by using OrderEntry as a join table between Order and all 3 XxxEntry models, but this means that order_entries table should then have 3 foreign key fields:
- product_entry_id
- shipment_entry_id
- discount_entry_id
This is the best solution I have so far, but it's not ideal, since there is a possibility of one Entry being associated to many Orders, which would be an error. Also, one joinTable is holding 3 foreign keys (and thus 3 associated models), which cannot be "NOT NULL", since only one should be filled while other 2 stay null for each record.
To conclude, what is the best practice for having a basic model, which is extended by several different models with their own tables?