-1

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' )
);
endo.anaconda
  • 2,449
  • 4
  • 29
  • 55

2 Answers2

1

the following was missing in the controller

$this->Staffgroup->recursive = 1;
endo.anaconda
  • 2,449
  • 4
  • 29
  • 55
0

Add

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

);

now when you retrieve information in usersgroup controller , it will contain information of User-group leaders.

Meet Brahmbhatt
  • 57
  • 1
  • 1
  • 8