【问题标题】:Cakephp3 multiple databasesCakephp3 多数据库
【发布时间】:2015-09-27 12:32:38
【问题描述】:

我想问一下,是否可以在 cakephp3 中使用原始 sql 处理 2 个数据库?

我有这样的查询:

select * from shop.brochures b, upload.documents up where b.doc_id = up.id;

问题是,这是 2 个数据库。

我不知道,如何设置连接,我认为这不起作用(文档示例)

$conn = ConnectionManager::get('default');

感谢您的任何建议。

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

    在 /config/app.php 中,您可以根据需要设置任意数量的数据库;我有 2 个。

    'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'username' => 'root',
            'password' => '',
            'database' => 'sisarticulos',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
        ],
    'gente' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'username' => 'root',
            'password' => '',
            'database' => 'sggeneral',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'cacheMetadata' => true,´
    

    然后在控制器中可以设置

    $conn = ConnectionManager::get('default');
    //some code
     $conn = ConnectionManager::get('get');
    

    先用默认,再用绅士

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多