CakePHP别名打破了HABTM模型

时间:2012-11-23 04:13:52

标签: php cakephp alias has-and-belongs-to-many cakephp-appmodel

考虑CakePHP 2.2.3中的以下HABTM关系:

class User extends AppModel
{    
    public $hasAndBelongsToMany = array(
        'Role' => array(
            'className' => 'Role',
            'joinTable' => 'roles_users',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'role_id',
        )

    );
}

这样可以正常工作,但是当使用VeryUniqueAlias而不是Role这样的别名并相应地更改UsersController时,m:n关系不会保留在数据库中(传递给{{1的数据)在控制器中等效)。

这不起作用:

save()

这很尴尬,因为它应该有CakePHP docs状态。 知道为什么它不适合我吗?我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

使用'with'键定义连接表的模型名称。在你的情况下:

public $hasAndBelongsToMany = array(
    'VeryUniqueAlias' => array(
        'className' => 'Role',
        'joinTable' => 'roles_users',
        'with' => 'RolesUser',    // first model pluralized
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'role_id',
    )

);