2013-09-26 42 views
1

我正在研究使用多個數據庫的蛋糕PHP的應用程序。我需要從多個表中獲取數據,我使用bindModel來進行關聯。但bindModel不允許數據庫切換功能,我需要從多個數據庫訪問數據。如果任何人已經完成了這種類型的任務,然後plz幫助我。在CakePHP中使用BindModel進行數據庫切換

+0

這可能會幫助你http://stackoverflow.com/a/13224580/1868660 –

回答

0

在你AppModel

class AppModel extends Model 
{ 
    /** 
    * Connects to specified database 
    */ 
    public function setDatabase($database, $datasource = 'default') 
    { 
     $nds = $datasource . '_' . $database;  
     $db = &ConnectionManager::getDataSource($datasource); 

     $db->setConfig(array(
     'name'  => $nds, 
     'database' => $database, 
     'persistent' => false 
    )); 

     if ($ds = ConnectionManager::create($nds, $db->config)) { 
     $this->useDbConfig = $nds; 
     $this->cacheQueries = false; 
     return true; 
     } 

     return false; 
    } 
} 

和控制器可以使用

class CarsController extends AppController 
{ 
    public function user() 
    { 
    $this->User->setDatabase('testdb1'); 
    $cars = $this->User->find('all'); 
    $this->set('User', $User); 
    } 

    public function client() 
    { 
    $this->Client->setDatabase('testdb2'); 
    $cars = $this->User->find('all'); 
    $this->set('Client', $Client); 
    } 

} 
+0

我已經使用這個配置,但我需要在使用bindModel函數時切換數據庫。如果我寫代碼和切換數據庫中的簡單查詢比我的工作,但如果我需要bindModel然後它不起作用。我想現在你得到了我的問題 –

+0

它不適用於me.pls建議任何其他的選擇。 –