Question

I'm a bit confused in how CakePHP does it's database relationship.

For a hasOne relationship, according to the documentation:

"User hasOne Profile"

User hasOne Profile -> profiles.user_id

Array
(
    [User] => Array
        (
            [id] => 121
            [name] => Gwoo the Kungwoo
            [created] => 2007-05-01 10:31:01
        )
    [Profile] => Array
        (
            [id] => 12
            [user_id] => 121
            [skill] => Baking Cakes
            [created] => 2007-05-01 10:31:01
        )
)

But isn't this a one to many relation?

For example (I made these table to illustrate my confusion):

enter image description here

enter image description here

In this case then there are 2 profiles belonging to the same user. Doesn't this mean a user hasMany profile?

Would it make more sense if "User hasOne Profile" is

enter image description here

enter image description here

So then this would be "User hasOne Profile" but "Profile hasMany User"?

I'm not sure if I'm understanding this correctly.

Was it helpful?

Solution

In the example at the documentation when its said that a user has one profile it means that a user can only have one profile.

Therefor is a one to one relationship. You can choose where you prefer to have the foreign key, and they preferred to use it on the profile table.

Your images are wrong. In your case it would be a one to many. Which is a hasMany + belongsTo. They just decided that there will be only one profile per user, if you want to have many profiles per user, then it won't be a hasOne. That's your decision.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top