Search code examples
phpyiimultiple-databases

Yii PHP : dynamic database connections for read/write data


i am developing a app that scans/crawls/monitors certain websites

the app is available for free at www.linkbook.co

now, i will explain what i want to do:

I have a main DB, that stores the websites, the urls found on the websites and url patterns from each website; The website knows the Db that he has been assigned to, the url knows the website id;

Because i have a 2 GB / DB limit, i need secondary DB's.

I read all from http://www.yiiframework.com/wiki/123/multiple-database-support-in-yii/ and what Google could find, and there is not a single complete example using dinamicaly the connection;

So, each time my app scans a website, each url found will be inserted in the main DB, and also in the DB that has been assigned to the website that the url belongs to.

In time, the data from the main DB gets deleted, but it is still available in the secondary DB.

So, only the HOT info gets stored in the main but they remain saved in the secondary db's

The Yii guys give this example, but i need a parameter, from 1 to N, N=1->infinite,in order to switch to the right DB.

class MyActiveRecord extends CActiveRecord {
    ...
    private static $dbadvert = null;

    protected static function getAdvertDbConnection()
    {
        if (self::$dbadvert !== null)
            return self::$dbadvert;
        else
        {
            self::$dbadvert = Yii::app()->dbadvert;
            if (self::$dbadvert instanceof CDbConnection)
            {
                self::$dbadvert->setActive(true);
                return self::$dbadvert;
            }
            else
                throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
        }
    }
    ...

$dbadvert needs to be dinamic, this is my problem.

also, the tables from the secondary DB's are not exactly the same, as i dont need all the fields from all the tables, and some tables get dropped also, so i need a model;

the model i can write it, it's not hard, i will just delete some fields;

this is what i have now, just the insert, in a specific DB, db1 a.k.a. linkbookco1

$command = Yii::app()->db1->createCommand("INSERT INTO `url` (
`id` ,
`website_id` ,
`link` ,
`name` ,
`created` ,
`instance_scanner_id` ,
`status`
)
VALUES (
NULL ,  '".($model_url->website_id)."',  '".($model_url->link)."',  '".($model_url->name)."',  '".($model_url->created)."',  '".($model_url->instance_scanner_id)."',  '".($model_url->status)."'
);
");
$command->query();

db1 and it's params are mentioned in the config file, as the yii developers say;


Solution

  • from your example I can guess you have a secondary database connection configured in your app like

    'dbadvert' => array(
        'class' => 'CDbConnection',
        ...
    )
    

    the point where you need to get the database connection you have this:

    self::$dbadvert = Yii::app()->dbadvert;
    

    So, to have multiple database connections you'd need to add them to your database configuration

    'dbadvert1' => array(
        'class' => 'CDbConnection',
        ...
    )
    'dbadvert2' => array(
        'class' => 'CDbConnection',
        ...
    )
    

    And in your code you can do something like

    self::$dbadvert = Yii::app()->getComponent("dbadvert".Yii::app()->request->getQuery('dbid', ''));