Martin
Martin

Reputation: 1340

foreign keys incongruity to save in cakephp

database design

I have this problem when I create phps with cake bake In cakephp.I have the following problem: if I keep two elements in libraries, one with id 1 and othe id 2.then I saved an item in rooms with library_id= 1,and after another item in rooms with library_id= 2. I really can relate room element with the element of user. This should not afford it since they have the same library_id.So it does not refer to the same library. I need only user can relate to room if they refer to the same library.I try that cakephp throws error, or do not allow the relationship, when users and rooms not have the same library_id,how I can do this?

result tables:

libraries         users               rooms                 users_rooms
id            id   library_id       id   library_id        user_id   room_id
1             1       1             1       2                1           1
2

Upvotes: 0

Views: 153

Answers (1)

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52107

I need only user can relate to room if they refer to the same library. how I can do this?

Normally, diamond-shaped dependencies where the two "sides" of the diamond must lead from the "bottom" to the same "root", require the usage of identifying relationships:

enter image description here

Note how library_id is migrated from the "root", down both "sides" of the diamond and then merged at the "bottom" in the users_rooms table. This way, if an user is connected to a room, they both must be connected to the same library.

Unfortunately, I'm not familiar with CakePHP so I can't tell whether it supports this.

Upvotes: 1

Related Questions