一个模型中的两个关系

时间:2012-07-12 14:55:08

标签: cakephp relationship

database relation

您好

我在上面创建了数据库关系,因为您看到一个人(工作人员)可以是许多组的成员。每个小组都有一个小组负责人。如何正确管理这种关系?

我会感谢每一个提示

这是我之前尝试过的:

员工模型:

    /**
 * 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' => ''
        )
    );

2 个答案:

答案 0 :(得分:0)

我认为你与belongsTo有一个不必要的关系。使用HABTM应该足以处理“一个人可以成为许多群体的一部分”的关系

有关详情,请查看Cookbook

答案 1 :(得分:0)

hasMany模型中缺少Staff个关联来表示StaffStaffGroup之间的“群组领导”关系。