This is one of the things I was trying to solve. In the current, official version of DataMapper, you have to have a column for every model type when working with multiple relationships (to the same table).
In other words, you need this as your join table:
join_files_users
- id NOT NULL
- file_id NOT NULL
- user_id
- creator_id
Note that they must allow nulls. Now the queries will work.
However, there is a big problem with this. If you look up the creator ($file->creator->get()), that creator will not work for any relationships that were set as a user. Assuming that Users have a relationship to Roles, this will not work:
$creator = $file->creator->get();
$role = $creator->role->get(); // there is no relationship here, because it will look for the column creator_id again.
$user = new User();
$user->get_by_id($creator->id);
$role = $user->role; // this works, nowIn the updated version, you can assign the same exact model in multiple ways, just using different field names:
class User extends DataMapper {
$has_many = array(
'file',
'created_file' => array(
'class' => 'file',
'other_field' => 'creator'
);
}
class File extends DataMapper {
$has_one = array(
'user',
'creator' => array(
'class' => 'user',
'other_field' => 'created_file'
);
}
// in controller
$creator = $file->creator->get(); // $creator is a User class
$user = $file->user->get(); // dittoWhat’s even better, you can create the file table like this:
files
- id,
- ...
- user_id int32, Must allow nulls
- creator_id int32 Must allow nulls