I am developing an application in CakePHP. I need two databases in same function in same controller. In Invoice I need to to add data in invoices table but need students list to show from another database having students table.
public function add() {
if ($this->request->is('post')) {
$this->Invoice->create();
$this->request->data['Invoice']['created_by'] = $this->Auth->user('id');
if ($this->Invoice->save($this->request->data)) {
$this->Session->setFlash(__('The Invoice has been saved.'), 'default', array('class' => 'alert alert-success'));
}
return $this->redirect(array('action' => 'view',$Invoice_id));
}
// fetch students from different database.
$this->loadModel('Student');
$users = $this->Student->find('list',array('fields'=>array('Student.id','Student.name')));
}
I am using the public $useDbConfig = 'fees'; as second DB configuration but unable to get the data in same function. Please help.
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'admin',
'database' => 'inventory',
'prefix' => '',
//'encoding' => 'utf8',
);
// fetch students from fees controller.
public $fees = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'admin',
'database' => 'fees',
'prefix' => '',
//'encoding' => 'utf8',
);
}