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条回答 默认 最新

  • dongpu9481 2016-03-26 12:43
    关注

    You need to declare that your Students model will use a different database source. You are saying that you used the public $useDbConfig = 'fees'; with out mentioning in which model you used this property.

    Check this link

    We can then configure the datasource in our app/Config/database.php file by adding something like this:

    public $faraway = array( 'datasource' => 'FarAwaySource', 'apiKey' => '1234abcd', );

    Then use the database config in our models like this:

    class MyModel extends AppModel { public $useDbConfig = 'faraway'; }

    So fees looks like some database that should be used on the Invoice model:

    class Invoice extends AppModel {
        public $useDbConfig = 'fees'; 
    }
    

    and Students models should most probably stay on the default database

    class Students extends AppModel {
        public $useDbConfig = 'default';  // This line is optional. Even if you don't write this line your model will load data from the default database.
    }
    

    Maybe the databases are the other way around but I think you got the point.

    You can still configure the database source of your models from inside your Controller by doing the following:

    public function add() {
        ...
        $this->Student->useDbConfig = 'fees'
        ...
    }
    

    or most preferable

    public function add() {
        ...
        $this->Student->setDataSource('default')
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?