多个数据库连接Yii2无法正常工作 [英] Multiple database connection Yii2 not working

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

问题描述

Yii::setAlias('@tests', dirname(__DIR__) . '/tests');

$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');
$dbPusat = require(__DIR__ . '/dbPusat.php');
$dbPaperless = require(__DIR__ . '/dbPaperless.php');

return [
    'id' => 'basic-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log', 'gii'],
    'controllerNamespace' => 'app\commands',
    'modules' => [
        'gii' => 'yii\gii\Module',
    ],
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
        'dbPusat' => $dbPusat,
        'dbPaperless' => $dbPaperless,
    ],
    'params' => $params,
];

在我的console.php中,我有3个连接,分别是db,dbPusat和dbPaperless。
用于db和dbPusat,它的工作完美。但对于dbPaperless,未检测到。当我想从该connectio生成模型时,会出现这样的错误。

In my console.php, i have 3 connection there is db, dbPusat and dbPaperless. for db and dbPusat its work perfectly. but for dbPaperless its not detected. When i want to generate model from that connectio there is an error like this.

请帮助我,谢谢。

推荐答案

从配置格式中,我假设您基于基本应用。在您的解释中,您提到在 console.php -config中,您定义了3个连接。那些仅适用于控制台。

From the configuration format I'm assuming that you are based on the basic application. In your explanation you mention that in "your console.php-config you have defined 3 connections". Those will work for console only.

您添加的屏幕截图清楚地显示了一个Web界面。因此,我只能得出结论,您没有在 web.php -config中添加相同的3个连接。

The screenshot you added clearly shows a web interface. Therefor I can only conclude that you haven't added the same 3 connections in the web.php-config.

当您在命令行上开始操作时,基本应用使用 console.php (通过 ./ yii ,当您通过浏览器访问该应用程序时,它将使用 web.php

The basic app uses console.php when you start an action on the command line (via ./yii and it uses web.php when you get to to the application via a browser.

所以基本上:将您的连接配置复制到 web.php 中,您就可以了。

So basically: Copy your connections configuration into the web.php and you should be ok.

这篇关于多个数据库连接Yii2无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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