Welcome Guest, Not a member yet? Register   Sign In
Multiple database connections in stensi's Datamapper
#1

[eluser]FabricioFirmino[/eluser]
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:

Code:
$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:

Code:
<?php

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

    function Sample()
    {
        
        parent::DataMapper();
    }
}

?>



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


[]s
#2

[eluser]FabricioFirmino[/eluser]
ANY??????
#3

[eluser]danmontgomery[/eluser]
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.

Code:
$this->database->load('first');
$this->load->library('datamapper');
#4

[eluser]FabricioFirmino[/eluser]
Thanks noctrum, i'll test, but, where i should put this code? In a controller or in model's constructor?
#5

[eluser]WanWizard[/eluser]
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 http://datamapper.wanwizard.eu.




Theme © iAndrew 2016 - Forum software by © MyBB