5

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',
        ],
    ],
];

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();

    }

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

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

Where have I done wrong ?

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('*'); 
    }
}

And I get this error

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

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

please help

Community
  • 1
  • 1
nodeffect
  • 1,830
  • 5
  • 25
  • 42
  • Seems to me this has nothing to do with Yii at all... "could not find driver" seems more like a `PDO` error. Are you sure all relevant php modules are installed? `PDO` alone is not enough, it needs `mysqlnd` as well iirc – Blizz May 26 '15 at 09:37
  • @Blizz, thanks for the tip, I've enable the pdo_mysql under PHP settings using WAMP.... That solves the PDO problem. – nodeffect May 26 '15 at 09:52
  • do you merge your db config with other config arrays ? – Tony May 26 '15 at 11:54

3 Answers3

5

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

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

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();
nodeffect
  • 1,830
  • 5
  • 25
  • 42
  • 1
    if using multiple db in master/slave mode then checkout the below link http://www.yiiframework.com/doc-2.0/guide-db-dao.html – kurmi Mar 11 '16 at 13:07
  • what if I use ActiveRecord instead of using `Yii::$app::db->createCommand..` for example if I have already used active record methods like `ModelName::find()->all()` in my app, now I want all my clients to use different db for their company. how and where do I tell the application which database to use? – Ramesh Pareek Mar 04 '17 at 16:20
3

You can also do this:

new User(array("db" => Yii::$app->db1));

or do this:

User::find()->where(...)->all(Yii::$app->db2);
fasisi
  • 396
  • 5
  • 23
1
  1. Make sure you have the php_mysql and php_pdo_mysql extensions installed.
  2. The \yii\db\ActiveRecord class should know what database it's working with

This is the code to you add to the model to make it aware of its database.

public static function getDb()
{
    return Yii::$app->get('db1');
}
Igbanam
  • 5,904
  • 5
  • 44
  • 68
  • what if I use ActiveRecord instead of using Yii::$app::db->createCommand.. for example if I have already used active record methods like ModelName::find()->all() in my app, now I want all my clients to use different db for their company. how and where do I tell the application which database to use? Does ActiveRecord classed always make a call to this function `getDb()` – Ramesh Pareek Mar 04 '17 at 16:21