2

how to set database configurable in active record ?

This my file : app\models\multi_db\Devices;

<?php
namespace app\models\multi_db;

use yii\db\ActiveRecord;

class Devices extends ActiveRecord
{ 
     private static $dbConn;

     public function __construct($config=[])
     {
        switch ($config['server']) {
        case '2':
            self::$dbConn = \Yii::$app->dbtwo;
            break;
        case '3':
            self::$dbConn = \Yii::$app->dbthree;
            break;          
        default:
            self::$dbConn = \Yii::$app->dbone;
            break;
        }
     }

     public static function getDb()
     {
        return self::$dbConn;
     }

     public function findDevice($id)
     {
        return self::findOne($id);
     }
}

This my code to get device :

$model = new Devices(['server' => 3]);
$device = $model->findDevice(1);

But this generate error:

Undefined index: server

I think the constructor not set in findDevice method?

How can I fix this?

4
  • Have you tried var_dump-ing $config, and commenting out the switch-case code? Maybe you are creating another instance somewhere else in your code? BTW, you probably also have to call the parent constructor at some point. Commented Nov 20, 2015 at 7:15
  • I don't get it, can you give me an example to dumping $config ? Becase if i commented out switch-case, the static getDb() not set and generate error for schema in Yii2. Thanks. Commented Nov 20, 2015 at 7:22
  • var_dump($config); and then you could do something like switch(1) to avoid the not-set issue, without triggering the error. Commented Nov 20, 2015 at 7:24
  • hi, thanks for helping me, the soju answer is working. Commented Nov 20, 2015 at 13:02

2 Answers 2

1

Well, findOne() will use your constructor without providing server config, that's why you get this error, you could simply try :

public function __construct($config=[])
{
    if (isset($config['server'])) switch ($config['server']) {
        case '2':
            self::$dbConn = \Yii::$app->dbtwo;
            break;
        case '3':
            self::$dbConn = \Yii::$app->dbthree;
            break;          
        default:
            self::$dbConn = \Yii::$app->dbone;
            break;
    }
    // don't forget to call parent constructor
    parent::__construct($config);
}

But I think the logic here is wrong, this should be set using a static method, e.g :

class Devices extends ActiveRecord
{

    private static $dbConn;

    public static function setServer($id)
    {
        switch ($id) {
            case '2':
                self::$dbConn = \Yii::$app->dbtwo;
                break;
            case '3':
                self::$dbConn = \Yii::$app->dbthree;
                break;          
            default:
                self::$dbConn = \Yii::$app->dbone;
                break;
        }
    }

    public static function getDb()
    {
        if (self::$dbConn===null)
            self::$dbConn = \Yii::$app->dbone;

        return self::$dbConn;
    }

}

Then :

// find device on dbone
$device = Devices::findOne(1);

// find device on dbtwo
Devices::setServer(2);
$device = Devices::findOne(1);
Sign up to request clarification or add additional context in comments.

Comments

1

Seems you have some mistake,

If you use findOne(id) mean you are getting to retrieve the value from DB. When you should create you server this way

$model = new Device();
$model->server = 3;
$model->save();

You have a mistake also in switch (you select always the same db ..dbtwo ) a think you should assign different db (dbOne, dbTwo, dbthree)

class Devices extends ActiveRecord
{ 
 private static $dbConn;

 public function __construct($config=[])
 {
    switch ($config['server']) {
    case '2':
        self::$dbConn = \Yii::$app->dbtwo;
        break;
    case '3':
        self::$dbConn = \Yii::$app->dbthree;
        break;          
    default:
        self::$dbConn = \Yii::$app->dbone;
        break;
    }
 }

 public static function getDb()
 {
    return self::$dbConn;
 }

 public function findDevice($id)
 {
    return self::findOne($id);
 }
}

4 Comments

Ok, that's my mistake for same db in switc statement. I've tried your code but still get the error. I've confused how to create 'server' config when using db statement, findOne and other. Thanks
Still same error, PHP Notice – yii\base\ErrorException Undefined index: server
But have you create the instance like suggest ?. How do you retrive the istance from db?.
Hi, thanks for helping me. the soju answer is work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.