Sunday, February 10, 2013

Model with arbitrary, extendable properties (through a hasMany properties table)

It is an age old question of having a parent model, whose properties can be "extendable". Something similar to Single-table-inheritance, but now really.

I don't know what the name for this pattern is, so please let me know if you do. I have first seen it in CakeDC's "UserDetails" model in the Users plugin, which implies it IS possible in Cake.

So here's an overly simplified example:

Car [model, make, year] hasMany Property [key, value, class]

Tire extends Property [allow_keys: diameter, width]
Engine extends Property [allow_keys: displacement, type]
Interior extends Property [allow_keys: seats, dash, aircon]

The background database rows for this example would be:

Car:
   [ model: beetle; make: volkswagen; year: 1956 ]
Properties:
[ key: Tire.diameter;       value: 15;       class: Tire ]
[ key: Tire.width;          value: 180;      class: Tire ]
[ key: Engine.displacement; value: 1200;     class: Engine ]
[ key: Engine.type;         value: petrol;   class: Engine ]
[ key: Interior.seats;      value: leather;  class: Interior ]
[ key: Interior.aircon;     value: false;    class: Interior ]

But you would save/retrieve it like this (helper methods):

array (
   'Car' => [model: beetle, make: volkswagen, year: 1956],
   'Tire' => [diameter: 15, width: 180],
   'Engine' => [displacement: 1200, type: petrol]
   ... your get the concept
)

Each model is responsible for its set of of fields (validation rules and key=>value typesetting), as well utilizing awesome callback functionality, etc.

The benefits:
- easily extendable data logic (just extend the model and define the fields you need)
- virtually unlimited and highly flexible properties
- speed (there is only one table join per Car request)
- easily cachable
- insta-setup (bind the association on-the-fly and you're ready to go)

Cons:
- not really a proper relational database design
- find conditions would be a bitch, but a different query approach might do the trick
- huge properties table
- Model was not really built for that... :)

Is there a solution for this (a behavior, anyone?). I've seen some some, which name themselves SingleTableInheritance or ClassTableInheritance, but this is not really STI.

If you feel the urge to go "no-no-no" on my ass, my requirement is to dynamically extend the properties with many different "property group" classes. If I resolve it through standard relations, I would have thousands of tables related to the basic model, holding just a few records each. There is also the issue of creating the tables dynamically.

I am in the middle of test-driving this model, so I thought it would be good to ask for the opinion of the experienced forum! :D Thoughts will be much appreciated.

Cheers!

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: