Kohana 3.2 ORM多对多 - 错误的字段名称

时间:2012-01-17 13:46:51

标签: orm kohana kohana-3.2

我尝试在两个模型之间建立多对多关系:Users_Role和Users_Right

class Model_Users_Role extends ORM{
    protected $_has_many = array(
        'rights' => array(
            'model'   => 'users_right',
            'through' => 'users_roles_rights',
        ),
    ); 
}

class Model_Users_Right extends ORM{    
    protected $_has_many = array(
        'roles' => array(
            'model'   => 'users_role',
            'through' => 'users_roles_rights',
        ),
    );
}

我正在尝试这样做:

$role = ORM::factory('users_role', 1);
$right = ORM::factory('users_right', 1);
$right->add('roles', $role);

错误:

Database_Exception [ 1054 ]: Unknown column 'role_id' in 'field list' [ INSERT INTO `users_roles_rights` (`users_right_id`, `role_id`) VALUES ('1', '1') ]

我试着在另一边做到:

$role = ORM::factory('users_role', 1);
$right = ORM::factory('users_right', 1);        
$role->add('rights', $right);

新错误:

Database_Exception [ 1054 ]: Unknown column 'right_id' in 'field list' [ INSERT INTO `users_roles_rights` (`users_role_id`, `right_id`) VALUES ('1', '1') ]

我希望ORM在数据透视表中使用users_role_idusers_right_id字段名称,但它使用错误的远密钥名称?我犯了哪个错误?

1 个答案:

答案 0 :(得分:2)

查看default values are set的位置。 试试这个:

class Model_Users_Role extends ORM{
    protected $_has_many = array(
        'rights' => array(
            'model'   => 'users_right',
            'far_key' => 'users_right_id',
            'through' => 'users_roles_rights',
        ),
    ); 
}

class Model_Users_Right extends ORM{    
    protected $_has_many = array(
        'roles' => array(
            'model'   => 'users_role',
            'far_key' => 'users_role_id',
            'through' => 'users_roles_rights',
        ),
    );
}

Kohana没有检查这种关系是否已经存在。

在数据库表中使用两个外键列上的复合唯一键强制执行此操作,或者在添加之前使代码检查该关系是否已存在。

相关问题