zoukankan      html  css  js  c++  java
  • phalcon 连接多个数据库 phalcon multi-database

    db:

    //This service returns a MySQL database  
    $di->set('dbMaster', function() {  
         return new PhalconDbAdapterPdoMysql(array(  
            "host" => "localhost",  
            "username" => "",  
            "password" => "",  
            "dbname" => ""  
        ));  
    });  
      
    //This service returns a PostgreSQL database  
    $di->set('dbSlave', function() {  
         return new PhalconDbAdapterPdoMysql(array(  
            "host" => "localhost",  
            "username" => "",  
            "password" => "",  
            "dbname" => ""  
        ));  
    }); 
    

      

    public function initialize()  
    {  
        $this->setConnectionService('dbMaster');  
        //or  
        $this->setConnectionService('dbSlave');  
    }  
    

      

    这是给出来的案例,在实际代码环境,用的是默认生成的模板文件。

    修改配置文件

    'database' => array(  
        'adapter'     => 'Mysql',  
        'host'        => '127.0.0.1',  
        'username'    => 'root',  
        'password'    => 'toor',  
        'dbname'      => 'db1',  
        'charset'     => 'utf8',  
    ),  
    'db2' => array(  
        'adapter'     => 'Mysql',  
        'host'        => '127.0.0.1',  
        'username'    => 'root',  
        'password'    => 'toor',  
        'dbname'      => 'db2',  
        'charset'     => 'utf8',  
    ),  
    

      

    $di->set('db', function () use ($config) {  
        return new DbAdapter($config->database->toArray());  
    });  
    $di->set('db2', function () use ($config) {  
        return new DbAdapter($config->db2->toArray());  
    

      

    在model中使用时,

    class user  extends Model  
    {  
        public function initialize()  
        {  
            $this->setConnectionService('db2');  
        }  
    }  
    

      

  • 相关阅读:
    delphi利用qdac qworker计划任务
    delphi libssh2 无法立即完成一个非阻止性套接字操作
    线程池底层原理
    【spring源码分析】二、Spring扩展点的归总
    【spring源码分析】一、spring Refresh流程
    Spring中常用的类
    设计模式-proxy
    SpringAOP
    SpringIOC
    难受,nginx worker进程内存持续飘升!
  • 原文地址:https://www.cnblogs.com/achengmu/p/7145516.html
Copyright © 2011-2022 走看看