Monday, June 1, 2009

A model that belongsTo one (and only one) of several other models

Hello, all.

I have a model for image uploads. Each upload belongs to one other
model, such as Car or House. Currently, I solve this by using one
hasAndBelongsToMany for each model that has images, which works, but
is obviously not ideal.

I see at least two possible, but not pretty, solutions to this:
* Have car_id, house_id etc. in the images table, which leaves you
with a lot of null values.
* Have one model for each model that can have an image like this:
car_image, house_image, which makes for a lot of unnecessary models.

I think the best idea would be to have a field called foreign_key or
somesuch in image, and another field called model, which determines
which model the foreign key connects to, but is this even possible?

Thanks in advance for all your help.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: