Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Multiple database connections
  • Hi there,

    I'd like to manage different database connections, here is my file config/db.php:

    return array(
       
        'mysql1' => array(
            'connection'  => array(
                'dsn'        => 'mysql:host=x.x.x.x;dbname=db1',
                'username'   => 'user1',
                'password'   => 'password1',
            ),
        ),
       
        'mysql2' => array(
            'connection'  => array(
                'dsn'        => 'mysql:host=x.x.x.x;dbname=db2',
                'username'   => 'user2',
                'password'   => 'password2',
            ),
        ),
    );


    whenever I try to select my database connection like :
    Database_Connection::instance('mysql1');
    I get
    Fuel\Core\FuelException [ Error ]:
    Database type not defined in "{$name}" configuration or "{$name}" configuration does not exist

    Is there anything wrong with my config file or my database connection usage?

    I refered to : http://stackoverflow.com/questions/7717778/fuelphp-and-multiple-database-connections but could not find a way to make it work.

    Thanks for your help
  • Is you db.php configuration correct? You say you added this to your app/config.php, which is the global database config. If a db.php also exists in your enviroment config folder (where this should be added), that file will overwrite the global one, causing your definitions not to be found.

    So, do not use app/config/db.php unless for global settings over all your environments.

    Note that you can also select the database a query runs on by passing it's name as a parameter of the execute() method: DB::select()->from('table')->execute('mysql2');

    This is optional, and not needed for the default database. For higher-layer functionality, like Model_Crud or Orm models you can configure the database to use in the model, for Auth it's defined in the config file.
  • I've tried to use the ->execute('mysql2') method but Fuel is still throwing the same exception.
    I've also tried to use my environment db.php file and paste the connection array: same result

    my db config looks correct. To prove it I just have to replace   'mysql1' => array(... by   'default' => array(... and using as natively ->execute(), It goes well and I am able to query the default db.

    any other idea?
  • HarroHarro
    Accepted Answer
    In your controller, before you access the db, do

    \Config::load('db', true);
    \Debug::dump(\Config::get('db'));

    What is the output? Is it what you think it is?
  • I could finally find my error. The Dump helped to understand what was wrong.

     In my environment db.php file I redeclare my connection as:
     'mysql_dro' => array(
        'type'           => 'mysqli',
        'connection'     => array(
            'hostname'       => 'x.x.x.x',
            'port'           => '3306',
            'database'       => 'mysql1',
            'username'       => 'user',
            'password'       => 'xxxxx',
            'persistent'     => false,
            'compress'       => false,
        ),
        'identifier'     => '`',
        'table_prefix'   => '',
        'charset'        => 'utf8',
        'enable_cache'   => true,
        'profiling'      => false,
        'readonly'       => false,
    ),

    I also set the active db like this :  
    Database_Connection::instance('mysql1');
    Config::set('db.active', 'mysql1');
    Then I could dump and  saw that the db.active variable was not set to the default anymore!

    Finally, I could query my db using: ->execute()

    It works fine now!
    Thanks Harro for your useful advices!
  • Hey,
    Hope you're Ok.
    never fixed this!
  • What problem?

    Use two different databases at the same time? Or two different database servers at the same time?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion