We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Specify a database to certain migrations

Hi, I'm trying for a project that uses 2 differents DBs to run generations on a DB wich is not the default one.

My models are already working fine using : config.php

return new \Phalcon\Config(array(
  'database'    => array(
    'adapter'   => 'Mysql',
    'host'      => 'localhost',
    'username'  => 'root',
    'password'  => '',
    'dbname'    => 'dttp11expl01j',
    'charset'   => 'utf8'
  ),
  'db_params'   => array(
    'adapter'   => 'Mysql',
    'host'      => 'localhost',
    'username'  => 'root',
    'password'  => '',
    'dbname'    => 'dttp11expl02j',
    'charset'   => 'utf8'
  ),

And in my models

class Users extends \Phalcon\Mvc\Model {
  public function initialize(){
    $this->setConnectionService('db_params');
  }

But I cannot find any info on how to run a migration that uses this db_params.

have you tried by registering the database service through the Di component

     $di->set('db_params', function () use ($config) {
         return new DbAdapter(array(
        'host' => $config->db_params->host,
        'username' => $config->db_params->username,
        'password' => $config->db_params->password,
        'dbname' => $config->db_params->dbname
        ));
    });

Hi, yes I have done this in my services.php file.

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->set('db', function () use ($config) {
    return new DbAdapter(array(
        'host' => $config->database->host,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname' => $config->database->dbname,
        "charset" => $config->database->charset
    ));
});

/**
 * Création d'un service nommé db_params utilisé dans les models liés à la DB de parametrage (02j)
 */
$di->set('db_params', function () use ($config) {
    return new DbAdapter(array(
        'host' => $config->db_params->host,
        'username' => $config->db_params->username,
        'password' => $config->db_params->password,
        'dbname' => $config->db_params->dbname,
        "charset" => $config->db_params->charset
    ));
});

My models are perfectly working with this DB. But I cannot figure how to force my migrations of the tables that use this DB to use this service.

have you tried by registering the database service through the Di component

   $di->set('db_params', function () use ($config) {
        return new DbAdapter(array(
       'host' => $config->db_params->host,
       'username' => $config->db_params->username,
       'password' => $config->db_params->password,
       'dbname' => $config->db_params->dbname
       ));
   });