@Daniel H
Currently, there is no way to have the different relationships to the same object without subclassing the duplicated object, as if it were a self-reference.
It depends on what your needs are, but the basic way to do this is to create your objects like so:
class User extends DataMapper {
$table = 'users'; // here for clarity, not required
$model = 'user'; // ditto
// add in validation, etc.
}
class EditingUser extends User { // note, extends User!
$table = 'users'; // required!
$model = 'editinguser'; // here for clarity, not required
// nothing else is needed in this class
}
class Task extends DataMapper {
$has_one = array( 'user', 'editinguser' );
}Now your table is a little trickier:
-- this joining table is named after the TABLE for users
CREATE TABLE tasks_users (
id SERIAL NOT NULL,
task_id integer NOT NULL,
user_id integer,
editinguser_id integer
);That table will handle both of the joins.
There is one important drawback: the EditingUser cannot be used for other join operations! DataMapper will always look for editinguser_id on the joins, so you can’t, for example do this:
$task = new Task();
$task->get_by_id(10);
// assume a user has multiple phone numbers
$task->editinguser->get()->phonenumber->get();That will fail because there is no editinguser_id column on the phonenumbers_users table.
@bEz
You don’t need to shout it 😉, but DataMapper does not have any built-in mechanisms for dealing with multiple DataBases. What is it, exactly, you are trying to achieve? Cross DB queries? Look up in one then the other?
DataMapper uses the same DataBase library as CI, so you might be able to hack something in by replacing the Model’s db element:
// do queries on default DB
$obj = new Object();
$obj->get();
// now query alternate database
$obj->db = $this->load->database('other_db', TRUE);
$obj->get();
// obj2 will still be using the default database.
$obj2 = new Object();If you ALWAYs need a different database for a specific model, you could create a custom constructor, like so:
class Thing extends DataMapper {
function Thing() {
parent::DataMapper();
$CI =& get_instance();
$this->db = $CI->load->database('otherdb', TRUE);
}
}(I haven’t tested this, these are just ideas that might work for you)