douyu0792
2016-03-26 12:14
浏览 25
已采纳

在控制器CakePHP中使用同一函数中的两个数据库

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',
    );
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题