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')
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题