
Hello
I created the database relation above, as you see one person (staff) can be member of many groups. Every group has a groupleader. How do I manage that relation correctly ?
I’d be thankfull for every hint
Here is what I tried before:
Staff Model:
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Staffgroup' => array(
'className' => 'Staffgroup',
'foreignKey' => 'Staffgroup_groupLeader',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Staffgroup' => array(
'className' => 'Staffgroup',
'joinTable' => 'staff_staffgroups',
'foreignKey' => 'staff_id',
'associationForeignKey' => 'staffgroup_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
Model Staffgroup
/**
* hasOne associations
*
* @var array
*/
public $belongsTo = array(
'Staff' => array(
'className' => 'Staff',
'foreignKey' => 'groupLeader',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Staff' => array(
'className' => 'Staff',
'joinTable' => 'staff_staffgroups',
'foreignKey' => 'staffgroup_id',
'associationForeignKey' => 'staff_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
There is a
hasManyassociation missing in yourStaffmodel to represent the “group leader” relationship betweenStaffandStaffGroup.