Phalcon Connecting multiple databases

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.