yii2上的多个数据库连接 [英] Multiple Databases Connection on yii2

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

问题描述

我正在尝试在yii2框架上使用多个数据库连接。在config文件夹中的db.php文件下,我有这段代码:

I'm trying to use multiple database connection on yii2 framework. Under my db.php file inside the config folder, I have this piece of code:

return [
    'class' => 'yii\db\Connection',
    'components' => [
        'db1' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=new',
            'username' => 'root',
            'password' => 'password',
            'charset' => 'utf8',
        ],
        'db2' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=old',
            'username' => 'root',
            'password' => 'password',
            'charset' => 'utf8',
        ],
    ],
];

在我的test.php中,在models文件夹下,我在下面...

In my test.php under the models folder, I have this below...

namespace app\models;

use Yii;
use yii\base\Model;
use yii\db\Query;

class GetAds extends Model
{
    public function ads()
    {

        $test = Yii::$app->db1->createCommand((new \yii\db\Query)->select('*')->from('members'))->queryAll();

    }

当我尝试访问时,出现此错误消息获取未知属性:yii\web\Application :: db1

when I try to access, I get this error message "Getting unknown property: yii\web\Application::db1"

如何解决此问题?我实际上已经按照本指南多个数据库连接和Yii 2.0

How do I solve this problem ? I've actually followed this guide Multiple database connections and Yii 2.0

我在哪里做错了?

最糟糕的是,我已设置为仅使用一个数据库...

The worst thing is, I've set to use just one database... and on my model, I use this code..

    namespace app\models;

    use Yii;
    use yii\base\Model;
    use yii\db\ActiveRecord;
    use yii\db\Query;

    class GetAds extends ActiveRecord
    {
        public static function tableName()
        {
            return 'ads_page';
        }

        public static function ads()
        {

            $count=(new \yii\db\Query)->from('ads_page')->count('*'); 
    }
}

我收到此错误

Database Exception – yii\db\Exception
could not find driver
↵
Caused by: PDOException
could not find driver

为什么这么难使用yii2?我从这里开始全部关注 http://www.yiiframework.com /doc-2.0/guide-db-dao.html

Why is using yii2 so hard ? I've follow all from here http://www.yiiframework.com/doc-2.0/guide-db-dao.html

请帮助

推荐答案

我已经解决了这个问题。

I've solved this problem. This might help others who need this.

在config / web.php下,我添加了以下行: 'db2'=> require(__ DIR__ 。'/db2.php'),放在此语句 'db'=> require(__ DIR__。'/db.php'),(不带引号)

Under config/web.php, I added this line "'db2' => require(__DIR__ . '/db2.php')," just under this statement "'db' => require(__DIR__ . '/db.php')," (without quotes)

,并使用与db.php中相同的代码在config文件夹下创建另一个新的db2.php文件:

and created another new db2.php file under the config folder using the same codes as in db.php:

<?php

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=test2',
    'username' => 'root',
    'password' => 'password',
    'charset' => 'utf8',
];

并调用第一个数据库。我用这个:

and to call the first database. I use this:

$row = Yii::$app->db->createCommand("SELECT * FROM test")->queryOne(); 

要从第二个数据库查询表,我使用以下方法:

To query table from the second database, I use this:

$row = Yii::$app->db2->createCommand("SELECT * FROM test2")->queryOne();

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

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