Search code examples
cakephpmodel-associations

Multiple Model Associations to same table


My situation: I want to bring a HABTM and a hasOne relation working in the same model. unfortunatelly its not loading the groupleaders userinformation into the usergroups.

  • a user can have multiple usergroups
  • a usergroup can have one groupleader (user id)

any help apreciated

I'm having a table layout like this:


users

id
username

user_usergroups

id
user_id
usergroup_id

usergroups

id
groupname
groupleader (value is user_id)

usergroups model

public $hasAndBelongsToMany = array(
    'User' => array(
        'className' => 'Users',
        'foreignKey' => 'usergroup_id',
        'associationForeignKey' => 'user_id'
    )
);


public $hasOne = array(
    'GroupLeader' => array( 'className' => 'User',
    'foreignKey' => 'usergroup_groupleader' )
);

Solution

  • the following was missing in the controller

    $this->Staffgroup->recursive = 1;