Kohana 3.2 ORM多对多-字段名称错误
我试图在两个模型之间建立多对多关系:用户\角色和用户\权限Kohana 3.2 ORM多对多-字段名称错误,orm,kohana,kohana-3.2,Orm,Kohana,Kohana 3.2,我试图在两个模型之间建立多对多关系:用户\角色和用户\权限 class Model_Users_Role extends ORM{ protected $_has_many = array( 'rights' => array( 'model' => 'users_right', 'through' => 'users_roles_rights', ), ); } class
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\u role\u id
和users\u right\u id
字段名,但它使用了错误的远键名称?我在哪里犯了错误?看看错误在哪里。
试试这个:
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不会检查该关系是否已经存在
在数据库表的两个外键列上使用复合唯一键强制执行此操作,或者在添加之前让代码检查该关系是否不存在。我将“far\u key”添加到数组中,数据添加成功。但如果我第二次尝试添加相同的关系,kohana会在透视表中再添加一行。为什么?