I'm working on application in cake PHP which uses multiple database. I need to fetch data from multiple tables and i'm using bindModel for their association. But bindModel does not allow database switch functionality, I need to access data from multiple databases.If anyone has done this type of assignment then plz help me out.
Asked
Active
Viewed 308 times
1
-
this may help you http://stackoverflow.com/a/13224580/1868660 – Subodh Ghulaxe Sep 26 '13 at 12:41
1 Answers
0
In your 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;
}
}
and in your controller you can use
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);
}
}

Subodh Ghulaxe
- 18,333
- 14
- 83
- 102
-
I'm already using this config but i need to switch database while using bindModel function. If i write simple query in code and switch database than thats works for me but if i need bindModel then it does n't works. I think now u got my problem – Sanchit Singla Sep 26 '13 at 12:52