0

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?

Vanja D.
  • 834
  • 13
  • 20

1 Answers1

0

I'm not sure if this is what you are looking for, but there is a Cake Entity plugin that may be worth a look: https://github.com/kanshin/CakeEntity

tigrang
  • 6,767
  • 1
  • 20
  • 22
  • This seems like a great Plugin. I considered it a great disadvantage of Cake not to treat data as Objects, which would be a lot more intuitive from an OOP standpoint. But I'm not sure this solves my problem. Thanks anyway, I'm sure this Plugin will find use in my future development! :) – Vanja D. May 09 '12 at 03:01