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

How can i use multiple database in CLI task

HI There ,

I am writing migration script from one MySQL database to other MySQL DB using CLI task in phalcon . 
**What i am doing : **
    I am fetching records from one DB using below code 
    $connection = new Phalcon\Db\Adapter\Pdo\Mysql((array) $this->config->db1_database);
    $connection->connect();

    $sqlPF = "SELECT actual FROM " . $dbPHPF . "user WHERE var_name = 'split'";
    $users = $connection->fetchAll($sqlPF, Phalcon\Db::FETCH_ASSOC);

  Using this queri i am getting records set . after that I am tring to inser all those records using phalcon model 
   foreach ($users as $user)
    {
         $obj = new \Socialveo\Core\Models\User();
         $obj->save($user);
  }

Problem :  I am not getting success to insert single records  . 

Please help me thanks
edited Mar '15

Hi, you can define one or many connections,

$mysql = new Phalcon\Db\Adapter\Pdo\Mysql((array) $this->config->db1_database);
$postgresql = new Phalcon\Db\Adapter\Pdo\Postgresql((array) $this->config->db2_database);

//index.php //cli bootstrap
$di = new CliDI();

$config = //your config file

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->set('db', function () use ($config) {

    $config = array(
        "schema" => $config->database->schema,
        "host" => $config->database->host,
        "dbname" => $config->database->dbname,
        "username" => $config->database->username,
        "password" => $config->database->password
    );

    return new \Phalcon\Db\Adapter\Pdo\Postgresql($config);

});

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->set('db-other', function () use ($config) {

    $config = array(
        "host" => $config->database_cms->host,
        "dbname" => $config->database_cms->dbname,
        "username" => $config->database_cms->username,
        "password" => $config->database_cms->password
    );

    return new \Phalcon\Db\Adapter\Pdo\Mysql($config);

});

etc...

in your model

class Model extends Phalcon\mvc\model
{
    public function initialize()
    {
        //By default ""db"" is choose for phalcon, you may specify other connection in your 
        $this->setConnectionService("db-other");
    }
}


5.2k

Why use setShared ?

Hi, you can define one or many connections,

$mysql = new Phalcon\Db\Adapter\Pdo\Mysql((array) $this->config->db1_database);
$postgresql = new Phalcon\Db\Adapter\Pdo\Postgresql((array) $this->config->db2_database);

//index.php //cli bootstrap
$di = new CliDI();

$config = //your config file

/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db', function () use ($config) {

   $config = array(
       "schema" => $config->database->schema,
       "host" => $config->database->host,
       "dbname" => $config->database->dbname,
       "username" => $config->database->username,
       "password" => $config->database->password
   );

   return new \Phalcon\Db\Adapter\Pdo\Postgresql($config);

});

/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db-other', function () use ($config) {

   $config = array(
       "host" => $config->database_cms->host,
       "dbname" => $config->database_cms->dbname,
       "username" => $config->database_cms->username,
       "password" => $config->database_cms->password
   );

   return new \Phalcon\Db\Adapter\Pdo\Mysql($config);

});

etc...

in your model

class Model extends Phalcon\mvc\model
{
  public function initialize()
  {
      //By default ""db"" is choose for phalcon, you may specify other connection in your 
      $this->setConnectionService("db-other");
  }
}