2017-06-06 83 views
2

我在database.php中两(默认和测试)DB连接:如何在CakePHP 2.x中获得当前DB连接名称

public function __construct() { 
     $this->default = array(
      'datasource' => 'Database/Mysql', 
      'driver' => 'mysql', 
       'persistent' => false, 
       'encoding' => 'utf8', 
       'prefix' => 'shaufel_', 
       'host' => 'localhost', 
       'database' => 'db', 
       'login' => 'root', /*** replace this ***/ 
       'password' => 'root', /*** replace this ***/ 
     ); 

     //test db 
     $this->test = $this->default; 
     $this->test['database'] = $this->test['database'].'_test'; 
    } 

我怎样才能得到当前DB连接名称在我的模型?这意味着例如:if(current_db is testDB) do something。我使用CakePHP 2.9.7。

回答

1

我自己找到了:$this->getDataSource()->config['database']

相关问题