在CakePHP 3中连接多个数据库 [英] Connect with multiple database in CakePHP 3

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

问题描述

我想在其他数据库表中存储一些数据历史记录和日志,就将来的记录而言,它可能是非常大的数据库.此外,应支持跨数据库SQL引导.所以请帮我解决这个问题.

I want to store some data histories and logs in other database table it may be very large database in terms of records in future. Also cross database SQL joing should be supported. So please help me for solution for this.

预先感谢

推荐答案

您已按照以下步骤在同一个Cakephp应用程序中使用多个数据源.

You have follow below steps fo use multiple datasource within same cakephp application.

在Config/app.php中提及多个数据库源

您必须管理多个数据源配置,默认情况下,数据源保留主数据库,然后再添加一个数据源,我们可以说它是第二个数据源的历史记录.如下所述

You have to manage multiple data source configurations, in default data source keep main database, and make one more data source we can say it history for second data source. As mentioned below

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => '<host name>',
        'username' => '<database user>',
        'password' => '<database password>',
        'database' => '<database name>',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'flags' => [],
        'cacheMetadata' => true,
        'log' => false,
        'quoteIdentifiers' => false,
        'url' => env('DATABASE_URL', null),
    ],
    'history' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => '<host name>',
        'username' => '<database user>',
        'password' => '<database password>',
        'database' => '<database name>',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'flags' => [],
        'cacheMetadata' => true,
        'log' => false,
        'quoteIdentifiers' => false,
        'url' => env('DATABASE_URL', null),
    ]
]

在表类中指定数据源

Src/Model/Table/<AnyOtherSource>Table.php中,添加以下要使用历史记录数据源的方法.无需在需要使用默认数据源的地方添加以下方法,因为默认数据源CakePHP会处理它.

In Src/Model/Table/<AnyOtherSource>Table.php, Add below method, where you want to use history data source. No need to add below method where you need to use default data source, for default data source CakePHP will take care of it.

public static function defaultConnectionName() {
    return 'history';
}

CakePHP 3中的加入和模型关联

您可以在同一MySQL实例上访问其他数据库,方法是在文件名前面加上前缀 具有适当数据库名称的表.您必须指定表格 名称与database_name.table_name,为此,您需要在下面添加 每个表类中的代码.

You access other databases on the same MySQL instance by prefixing the table with the appropriate database name. You have to specify table name with database_name.table_name, For this you need to add below code in each table classes.

例如,

SELECT * FROM this_database.table_1 t1 JOIN that_database.table_2 t2 ON t2.column = t1.column

Src/Model/Table/<All>Table.php

class LogsTable extends Table {

    public function initialize(array $config) {
        parent::initialize($config);

        $this->table($this->connection()->config()['database'] . "." . $this->table()); // this is very important for joining and associations.

        // your other code for initilize method
    }

    public static function defaultConnectionName() {
        return 'history';
    }

    // other methods and your code should be here

}

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

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