Reputation: 166
I am using CakePHP 2.4 and I need to have a model called Files to store file information for Users, Cases, and Trucks. Now a file can be associated with many other models (Users, Cases, Trucks, etc). In other words I want Users to have Files, Cases to have Files, and Trucks to have Files.
Do I need to create UserFiles model, CaseFiles model, and TruckFiles or can I have a table entry that flags in Files that it is a User record, Case record, or Truck record? I also want to do the same for Notes and Tasks, so there would either be UserNotes, CaseNotes, and TruckNotes or can I have Notes (id, foreign_id, associatedModel(would be Truck, User, or Case), etc..). So that when I create my Users, I have Files that tie to my User.id and have a condition that the associatedModel is User.
I think I will need to create the UserFiles, UserNotes, and UserTasks. But if there is a good design and better way to do this I am open to suggestions.
Upvotes: 0
Views: 97
Reputation: 6205
CakePHP already has a Class named File. See: http://api.cakephp.org/2.4/class-File.html
For this its better to use other name, for example Attach.
User hasMany Attach
Case hasMany Attach
Truck hasMany Attach
Instead of create three foreign keys on 'attaches' table (like user_id, case_id, and truck_id), in this case i prefer create a model_name and model_id fields as foreign key.
schema table attaches:
- id
- model_name
- model_id
- ...
model Attach:
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'model_id',
'conditions' => array('Attach.model_name' => 'User'),
),
'Case' => array(
'className' => 'Case',
'foreignKey' => 'model_id',
'conditions' => array('Attach.model_name' => 'Case'),
),
'Truck' => array(
'className' => 'Truck',
'foreignKey' => 'model_id',
'conditions' => array('Attach.model_name' => 'Truck'),
),
);
model User, Case, Truck:
public $hasMany = array(
'Attach' => array(
'className' => 'Attach',
'foreignKey' => 'model_id',
'dependent' => true,
'conditions' => array('Attach.model_name' => 'User'), // Change the condition on Case and Truck model
),
);
Upvotes: 1
Reputation: 3823
One way to think about it is if the relationship is one to many (hasMany), many to one (belongsTo), one to one (hasOne), or many to many (hasAndBelongsToMany). With the first three mentioned, you can store the foreign key in one of the models because at least one of the models being associated is associated with only one entry in the other table. In the many to many relationship, you need a join table to store the pairs of foreign keys representing the associations.
In this case, if you're talking about a file that can belong to many users, and users can have many files, then that is a many to many or hasAndBelongsToMany relationship, and then yes, you need a UsersFile join table. If however, a file can only belong to one user, or a user can only have one file, then you're talking about one of the other relationships, and you do not need the join table. The same can be said for files/cases and files/trucks.
Upvotes: 0
Reputation: 3879
You would need to create these models
Then, you define model associations as you need.
For example: User hasMany File, Case hasMany File, Truck hasMany File, File belongsTo User, Case and Truck
Upvotes: 0