我正在研究使用多個數據庫的蛋糕PHP的應用程序。我需要從多個表中獲取數據,我使用bindModel來進行關聯。但bindModel不允許數據庫切換功能,我需要從多個數據庫訪問數據。如果任何人已經完成了這種類型的任務,然後plz幫助我。在CakePHP中使用BindModel進行數據庫切換
1
A
回答
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建議任何其他的選擇。 –
相關問題
- 1. cakephp bindmodel與計數
- 2. CakePHP bindModel不工作
- 3. CakePHP bindModel HABTM保存
- 4. 在CakePHP中使用$ useDbConfig在readOnly和writeOnly數據庫之間切換?
- 5. CakePHP - 如何即時切換數據庫? (CakePHP 1.1.x)
- 6. CakePHP中連接兩個表使用bindModel方法CakePHP的
- 7. 使用D3中的單選按鈕進行數據切換
- 8. 如何在cakephp和postgres數據庫中進行行鎖定?
- 9. 切換數據庫
- 10. Bindmodel綁定模型? CakePHP的
- 11. cakephp bindmodel限制問題db2
- 12. 如何在cakephp 3.x控制器中使用bindmodel?
- 13. 使用後臺進程在cakephp中備份大數據庫?
- 14. 在Codeigniter Mongo類中切換數據庫
- 15. 在LinqToSql中切換數據庫
- 16. 在rails中切換數據庫階段
- 17. 在JBoss中動態切換數據庫
- 18. 在Mezzanine中切換數據庫後端
- 19. 在CakePHP3中動態切換數據庫
- 20. CakePHP:使用bindModel的相關模型的加載計數
- 21. 在cakephp 2.4中使用路由前綴進行數據庫選擇?
- 22. 在CakePHP中使用不同的MySql數據庫進行寫操作
- 23. 使用jquery執行數據切換
- 24. 在運行時在Mongoid 3.0中切換數據庫
- 25. 在cakePHP中使用Ajax獲取數據庫中的數據
- 26. 在大型數據庫中進行數據交換
- 27. 切換數據庫值
- 28. 動態切換數據庫
- 29. 如何切換數據庫?
- 30. Rails切換數據庫
這可能會幫助你http://stackoverflow.com/a/13224580/1868660 –