cakephp3多个数据库连接,插件未加载

时间:2016-10-03 22:34:00

标签: cakephp multiple-databases cakephp-3.x cakephp-3.2

我正在尝试从使用不同数据库的插件模型加载一些数据。我在config / app.php中添加了一个条目,如下所示:

        /**
     * The test connection is used during the test suite.
     */
    'alternate1' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        //'port' => 'non_standard_port_number',
        'username' => 'alt_user',
        'password' => '********',
        'database' => 'secondary_db',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
        'log' => false,
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
        'url' => env('DATABASE_TEST_URL', null),
    ],

在我的插件/plugins/Importer/src/Model/LogsTable.php类中,我添加了

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

现在,我希望能够从我的主应用程序控制器访问这些数据,所以我尝试了这个:

/src/Controllers/ReviewController.php

中的

$this->loadModel('Importer.Logs');
$logCount = $this->Logs->find('all')->count();

如果我像这样检查模型:

print_r($this->Logs);

我发现它仍然使用'默认'连接,而不是'alternate1'连接。

Cake\ORM\Table Object ( [registryAlias] => Importer.Logs [table] => logs [alias] => Logs [entityClass] => \Cake\ORM\Entity [associations] => Array ( ) [behaviors] => Array ( ) [defaultConnection] => default [connectionName] => default ) 

我还尝试了TableRegistry :: get('Importer.Logs')和其他一些变体,但最后我无法让Cake正确加载正确的数据连接。

我在这里缺少一些简单的东西,我只知道它。答案:CakePHP 3 defaultConnectionName in plugin model doesn't work似乎是我想要的,但这不正确。我已经在使用Namespace.Model格式了。

0 个答案:

没有答案