Application_Model_User
在 Datamapper 中设计关系时,必然会将关系称为与相关对象同名的关系,当您有类似类名之类的东西时,这不太方便。对于那些急于说有一个带有“class”键的配置选项的人,我知道。去过那里试过。它仅适用于获取相关对象,不适用于更新它们。
这是重现问题的代码片段:
// User Model
class UserModel extends Datamapper
{
public $table = 'users';
public $has_many = array(
'roles' => array(
'class' => 'RoleModel',
'other_field' => 'usermodel',
'join_other_as' => 'role',
'join_self_as' => 'user',
'join_table' => 'users_roles'
),
);
}
class RoleModel extends DataMapper
{
public $table = 'roles';
public $has_many = array(
'usermodel' => array('class' => 'UserModel',
'other_field' => 'roles',
'join_other_as'=> 'user',
'join_self_as' => 'role',
'join_table' => 'users_roles' )
);
}
// controller code. Make sure you have a role with INT id = 2, and a user with INT id = 5 in your db
$user = new UserModel(2);
$role = new RoleModel(5);
$user->save($role);
此代码给出“无法将用户模型与角色模型相关联”。user_roles
错误,但是如果将关系从“roles”重命名为“rolemodel” ,它确实可以正常工作(意味着在连接表中插入了一条新记录)。
所以,如果有任何 CI 的 Datamapper 的狂热用户可以提供帮助,请告诉我如何正确定义关系。