0
votes

I have two models in a CakePHP application: Users and Groups. Initially I had not relationships between the two. Now in the database table groups, I manually add the column user_id so that now I have the following relationships between the two:

Groups belongsTo Users
Users hasMany Groups

Now after doing this for some reason I keep getting the following error:

1054: Unknown column 'Group.user_id' in 'field list'

I tried to run CakePHP's bake command to see if Cake will automatically detect the relationship between my two models. When I try to bake the User model, CakePHP does not even detect that there is a hasMany relationship between User and Group. Any idea why is this happenning and how I can fix this please?

Thank you

1
Try clearing your cache. - joshua.paling
which cache do I need to clear - user765368
In cake 2.x, delete everything inside app/tmp/cache/models/ I'm not sure about Cake 1.3, it might be slightly different. - joshua.paling

1 Answers

0
votes

Perhaps I am misunderstanding how you want your associations, but I would imagine that:

A User belongsTo a Group

A Group hasMany Users

Example User model:

class User extends AppModel {
    public $belongsTo = array(
        'Group' => array(
            'className' => 'Group',
            'foreignKey' => 'group_id'
        )
    );
}

Example Group model:

class Group extends AppModel {
    public $hasMany = 'User';
} 

    

Your Users table will need a group_id field.

All of this is assuming that you want one group per user. If you want Users to have multiple groups then you may wish to define a HABTM relationship between Users and Groups, or perhaps researching Access Control Lists may point you in a better direction (I have yet to utilize Cake's ACL component yet).

Either way, I highly suggest reviewing the Cakebook page on Associations: Linking Models Together.