Friday, April 25, 2014

CakePHP Polymorphic Association

Hello,

 

I've developed a fairly complex application on CakePHP 2.4.X, and at this point I've encountered this issue.

 

Say I have a model, called Cake. Cakes may be baked in-house or purchased from a factory.

 

Now, say that a Cake model instance has a belongs-to association to the "pastry cook" and "factory" models. This is trivial to set up using separate fields for each field.

 

My question is: how can I unify this so that I have an "origin_id" field that transparently maps to a "pastry cook" or a "factory", provided I can somehow differentiate them by the contents of the fields (say all cook ids start with "c" and factory ids, with "f")? I know this can be solved while reading in a fairly straightforward (though dirty) way using virtual fields and callbacks. My question is about doing this in a transparent way that also works for saving/validating cakes. I need not create new cooks or factories through saveMany/saveAll though.

 

Thanks,

Ricardo Vieitez | Student Assistant - Network Administration
University of Florida Department of Housing and Residence Education
PO Box 112100 | Gainesville, FL 32611-2100
office 352.392.2171 x
10355 | fax 352.392.6819 | RicardoV@housing.ufl.edu
StrengthsQuest Top 5: Analytical, Strategic, Learner, Responsibility, Relator - Find Out More
Please consider the environment before printing this email.

1 comment:

Unknown said...
This comment has been removed by the author.