与同一模型有多个HABTM关系

时间:2012-09-29 13:49:27

标签: php cakephp

在我的蛋糕应用程序中,我在用户模型上建立了几种关系,用于各种事物,如喜欢,代言,友谊等。

然而,Cake似乎不喜欢我这样做的方式因为我在数组中不止一次重复'User'。那我该怎么办?

public $hasAndBelongsToMany = array(
        'User'=>array(
            'className'              => 'User',
            'joinTable'              => 'friends',
            'with'                   => 'Friend',
            'foreignKey'             => 'user_id',
            'associationForeignKey'  => 'friend_id'
        ),
        'User'=>array(
            'className'              => 'User',
            'joinTable'              => 'endorsements',
            'with'                   => 'Endorsement',
            'foreignKey'             => 'user_id',
            'associationForeignKey'  => 'endorsed_id'
        ),
        'Interest' => array('with' => 'InterestUser')
    );

1 个答案:

答案 0 :(得分:3)

嗯,你不能在php中重复数组键,CakePHP无法做到这一点。对第二个关联使用不同的别名。简而言之,对于您的第二个关联,使用不同的密钥,例如“User2”。您可以在手册上获得有关模型别名的更多信息。