Datamapper でリレーションシップを設計する場合、リレーションシップを関連オブジェクトと同じ名前で呼ぶ必要がありますApplication_Model_User
。クラス名のようなものがある場合、これはあまり便利ではありません。「クラス」キーを使用した構成オプションがあると急いで言う人のために、私は知っています。そこに行ってみました。関連するオブジェクトを取得する場合にのみ機能し、更新する場合には機能しません。
問題を再現するためのコード スニペットを次に示します。
// 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);
このコードは、「usermodel を rolemodel と関連付けることができません」というメッセージを表示します。user_roles
エラーですが、リレーションの名前が「ロール」から「ロールモデル」に変更された場合、正しく機能します (結合テーブルに新しいレコードが挿入されることを意味します)。
したがって、CI の Datamapper の熱心なユーザーが助けになる場合は、関係を適切に定義する方法を教えてください。