Multiple databases basics not working

I have the following config:

$db['master_db']['hostname'] = "";
$db['master_db']['username'] = "root";
$db['master_db']['password'] = "";
$db['master_db']['database'] = "database_name";
$db['master_db']['dbdriver'] = "mysql";

$db['slave_db']['hostname'] = "";
$db['slave_db']['username'] = "root";
$db['slave_db']['password'] = "";
$db['slave_db']['database'] = "database_name";
$db['slave_db']['dbdriver'] = "mysql";

$active_group = "master_db";

And a model:

class Foo_model extends Base_module_model {
   protected $dsn = 'slave_db';
   function __construct(){}

This works fine from my controller if I'm doing something simple such as $data = foo_model->find_all() followed by outputting as a CSV.

If I want to use the $vars['data'] = foo_model->find_all() and then render it in a Fuel page:

function test() {
   $vars = array('page_title' => 'Test','layout'=>'test');
   $vars['data'] = $this->foo_model->find_all();
   $page_init = array('location'=>'location','render_mode'=>'view','cache'=>false);
   $this->fuel->pages->render("test_view", $vars, $page_init);

I get "Unable to connect to your database server using the provided settings."

I've traced into fuel->pages->render to Fuel_pages_model.php and if I add protected $dsn = 'master_db'; it passes that step (but still fails, I guess it would be fine if I added protected the same to the other Fuel models too).

It seems to me something basic is broken for this style of database configuration. Any suggestions?


  • It may be because you don't have a dsn of "default" set which is what the FUEL models will be looking for.

  • I noticed my ./config/database.php notation was old (I'm using 1.4.13) so have swapped in the new notation and renamed 'master_db' to 'default' (remembering the active group).

    It made no difference.

    I've just edited

    class Base_module_model extends MY_Model {
       protected $dsn = 'default';

    And that seems to have fixed it.

    Not sure if I've found a bug and fixed it or something else fundamental is broken in my setup...

  • I think that may be a bug. I've pushed an update to the develop branch which essentially does what you are doing.

Sign In or Register to comment.