Phalcon you need to configure multiple connections when connecting to multiple databases
' Tutorial ' = [ //' adapter ' = ' mysql ', ' host ' = ' 127.0.0.1 ', ' Port ' ' = ' = ' 3306 ', ' username ' + ' root ', ' password ' + ' , ' dbname ' and ' tutorial ' ], c11/> ' invo ' + [ //' adapter ' = ' mysql ', ' host ' = ' 127.0.0.1 ', ' port ' = ' 3306 ', ' username ' = ' root ', ' password ' + ', ' dbname ' = ' invo ' ],
Because in our database is generally MySQL, so here adapter can omit not to write, directly in the use of the time specified to MySQL, as follows:
Use Phalcon\db\adapter\pdo\mysql as Dbadapter;
This allows you to register for direct
$di->set (' Tutorial ', function () use ($config) {
return new Dbadapter ($config->tutorial->toarray ());
});
$di->set (' Invo ', function () use ($config) {
return new Dbadapter ($config->invo->toarray ());
});
Register two database connections, you need to connect to the database in models.
Public Function Initialize () {
$this->setconnectionservice (' Invo ');
}
method to specify which database to connect to;
Phalcon The official example to register the database when the code is this:
$di->set (' Tutorial ',function() Use($config) { return NewDbadapter (Array( ' Host ' =$config->tutorial->host, ' username ' =$config->tutorial->username, ' password ' =$config->tutorial->password, ' dbname ' =$config->tutorial->dbname);});$di->set (' Invo ',function() Use($config) { return NewDbadapter (Array( ' Host ' =$config->invo->host, ' username ' =$config->invo->username, ' password ' =$config->invo->password, ' dbname ' =$config->invo->dbname);});
This effect is the same as the above method, just split apart;
There is also a variant that can be selected for different database types
$di->set (' Tutorial ',function() Use($config) { $tutorial=$config->tutorial->ToArray (); $dbClass= ' phalcon\db\adapter\pdo\\ '.$tutorial[' Adapter ']; unset($tutorial[' Adapter ']); return New $dbClass($tutorial);});$di->set (' Invo ',function() Use($config) { $invo=$config->invo->ToArray (); $dbClass= ' phalcon\db\adapter\pdo\\ '.$invo[' Adapter ']; unset($invo[' Adapter ']); return New $dbClass($invo);});
This method needs to set the adapter setting database type in config.php, which can be used for connecting different database requirements;
Phalcon Connecting multiple databases