Tuesday, June 2, 2009

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

On Mon, Jun 1, 2009 at 5:41 AM, Petter <petter.vegsund.brodin@gmail.com> wrote:
>
> 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.


If I undestood corretly and if your models has only one upload,
the more adequate association should be that your models
know their related upload.

Did you try to put a foreignKey like upload_id in your Car, House
models? In this case your associations should be "Car belongsTo Upload"
and "Upload hasOne Car", "Upload hasOne House" and so on.

Best regards.

--
MARCELO DE F. ANDRADE
Belem, PA, Amazonia, Brazil
Linux User #221105

http://mfandrade.wordpress.com

--~--~---------~--~----~------------~-------~--~----~
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: