Multiple database connections in stensi's Datamapper

Hi everybody.

I'm using Stensi's datamapper and now i need connect in two distincts databases, so, i've configured my database.php like bellow:

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "user";
$db['default']['password'] = "password";
$db['default']['database'] = 'databalse';
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

$db['first']['hostname'] = "localhost";
$db['first']['username'] = "first_user";
$db['first']['password'] = "first_password";
$db['first']['database'] = 'first_databalse';
$db['first']['dbdriver'] = "mysql";
$db['first']['dbprefix'] = "";
$db['first']['pconnect'] = TRUE;
$db['first']['db_debug'] = TRUE;
$db['first']['cache_on'] = FALSE;
$db['first']['cachedir'] = "";
$db['first']['char_set'] = "utf8";
$db['first']['dbcollat'] = "utf8_general_ci";

$db['second']['hostname'] = "localhost";
$db['second']['username'] = "second_user";
$db['second']['password'] = "second_password";
$db['second']['database'] = 'second_databalse';
$db['second']['dbdriver'] = "mysql";
$db['second']['dbprefix'] = "";
$db['second']['pconnect'] = TRUE;
$db['second']['db_debug'] = TRUE;
$db['second']['cache_on'] = FALSE;
$db['second']['cachedir'] = "";
$db['second']['char_set'] = "utf8";
$db['second']['dbcollat'] = "utf8_general_ci";

My Model classes are like bellow:


class Sample extends DataMapper
    public $table = 'samples';

    function Sample()


How can i set which database (default, first or second) this model is mapped?



Datamapper inherits the database from whatever $this->db is in the CI superobject. If you autoload both, it will always be default. To specify another DB, you would have to load the db library and specify the database in the way you normally would.


Thanks noctrum, i'll test, but, where i should put this code? In a controller or in model's constructor?

First of all, I would suggest using the latest version of Datamapper (currently v1.8.0). Stensi's version is ancient, and hasn't been maintained for years.

Since v1.6.0 you can control database behaviour using the configuration setting 'db_params'. When specified in the datamapper config file, you can set a default behaviour for all models. You can override this on a per-model basis by defining the class property 'db_params' in your model, and assigning the required value to it. Any value other then "", TRUE or FALSE, will be passed on to $this->load->database() as first parameter.

I suggest you start by going through the manual first, as this is all documented. The manual is included in the download, or online at

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.