CakePHP 3多个数据库 [英] CakePHP 3 Multiple Databases

查看:77
本文介绍了CakePHP 3多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CakePHP 3连接到第二个(远程)数据库。我已经找到了在线解决方案,这些解决方案建议如何将不同的模型与不同的数据库相关联,但这不是我需要实现的。

I want to connect to a second (remote) database using CakePHP 3. I have found solutions online that suggest how to associate different models with different databases but that is not what I need to achieve.

我需要能够连接到远程数据库(不与任何模型关联),并从控制器中的操作读取/写入一些记录。可以使用CakePHP做到吗?

I need to be able to connect to a remote database (that is not associated with any model) and read/write some records from an action in my controller. Can this be achieved using CakePHP?

编辑(更多信息):

我有一个网站,可作为酒店房间的预订平台。这些房间的可用性可以通过我的网站进行控制,并存储在我的数据库中。但是对于某些客户,我希望能够直接连接到其私有数据库并使用其记录来检查可用性。

I have a website that acts as a booking platform for hotel rooms. The availability of these rooms can be controlled via my website and stored in my database. But for some clients I want to be able to connect to their private database directly and use their records to check availability.

推荐答案

遵循以下说明


步骤1:打开 config /app.php 找到 Datasources 数组并添加 Remote Database 配置,例如-

Step 1 : Open config/app.php Find Datasources array and add Remote Database configuration like as -



'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'port' => '3306',
        'username' => 'YOUR_DB_USER',
        'password' => 'YOUR_DB_PASS',
        'database' => 'YOUR_DB_NAME',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'flags' => [],
        'cacheMetadata' => false,
        'log' => false,
        'quoteIdentifiers' => false,
        'url' => env('DATABASE_URL', null),
    ],
    'remote_db_1' => [ /*Remote Database 1*/
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => '192.168.1.47', /*YOUR_REMOTE_SERVER_IP*/
        'port' => '3306',
        'username' => 'REMOTE_DB_USER',
        'password' => 'REMOTE_DB_PASS',
        'database' => 'REMOTE_DB_NAME',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'flags' => [],
        'cacheMetadata' => false,
        'log' => false,
        'quoteIdentifiers' => false,
        'url' => env('DATABASE_URL', null),
    ],
   'remote_db_2' => [ /*Remote Database 2*/
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => '192.168.1.47', /*YOUR_REMOTE_SERVER_IP*/
        'port' => '3306',
        'username' => 'REMOTE_DB_USER',
        'password' => 'REMOTE_DB_PASS',
        'database' => 'REMOTE_DB_NAME',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'flags' => [],
        'cacheMetadata' => false,
        'log' => false,
        'quoteIdentifiers' => false,
        'url' => env('DATABASE_URL', null),
    ],




步骤2:现在,您可以在控制器中访问远程数据库,例如as-

Step 2 : Now you can access Remote database in your controller like as-



use Cake\Datasource\ConnectionManager;
use \PDO;

class YourController extends AppController{
    public function getRemoteData(){
      $conn1 = ConnectionManager::get('remote_db_1'); #Remote Database 1
      $conn2 = ConnectionManager::get('remote_db_2'); #Remote Database 2
    }
}




注意:现在,您可以使用 PDO 方法来插入,检索,更新

Note: Now you can use PDO methods to Insert,Retrieve,Update

示例:

class YourController extends AppController{
    public function getRemoteData(){
      $conn1 = ConnectionManager::get('remote_db_1'); #Remote Database 1
      $sql   = "SELECT * FROM  users";
      $query = $conn1->prepare($sql);
      $query->execute();
      $result = $query->fetchAll(); #Here is the result
    }
}

这篇关于CakePHP 3多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆