How to code asscoiation between one table and mutlible others?


#1

Hi

This is my first attempt with CakePHP ever and my first development since long time ago so please bear with me :slight_smile:

My solution to a probelm is as follow:

I have a table/model called Contents that has the following fields:

id (PK)
lable
content
content_type
reference_id
model

This table contain some contents that is associated with the records in different models/tables. The ‘reference_id’ contains the id of the record in other tables, just like a foreign key. However it is not associated with a speciifc table but based on the value of ‘model’ I will know where should I look for the record.

Is this a good approach for CakePHP framework or should I have separte Contents table for each model that needs it? If it is good how should I code the association?

TIA

Captain


#2

What you are looking for is something called “Polymorphic Associations”. Take a look at Containing Polymorphic Associations for some ideas.