i developing application in cakephp. need 2 databases in same function in same controller. in invoice need to add data in invoices table need students list show 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 different database. $this->loadmodel('student'); $users = $this->student->find('list',array('fields'=>array('student.id','student.name'))); }
i using public $usedbconfig = 'fees'; second db configuration unable 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 fees controller. public $fees = array( 'datasource' => 'database/mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'root', 'password' => 'admin', 'database' => 'fees', 'prefix' => '', //'encoding' => 'utf8', ); }
you need declare students
model use different database source. saying used public $usedbconfig = 'fees';
out mentioning in model used property.
check link
we can configure datasource in our app/config/database.php file adding this:
public $faraway = array( 'datasource' => 'farawaysource', 'apikey' => '1234abcd', );
then use database config in our models this:
class mymodel extends appmodel { public $usedbconfig = 'faraway'; }
so fees
looks database should used on invoice model:
class invoice extends appmodel { public $usedbconfig = 'fees'; }
and students
models should stay on default database
class students extends appmodel { public $usedbconfig = 'default'; // line optional. if don't write line model load data default database. }
maybe databases other way around think got point.
you can still configure database source of models inside controller doing following:
public function add() { ... $this->student->usedbconfig = 'fees' ... }
or preferable
public function add() { ... $this->student->setdatasource('default') ... }
Comments
Post a Comment