Yii Framework Forum: Dynamic DB Connection - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Dynamic DB Connection Creating DB Connection Dynamically Rate Topic: -----

#1 User is offline   Merrill 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 26-October 09

Posted 02 November 2009 - 04:56 PM

I'm starting a new development.

I have multiple clients (companies) and each client has multiple users.

Each client has their own database, so during user authentication, I discover the name of associated database for that user.

The structure of each database is identical... only the data is different.

The number of clients (and therefor the number of databases) is unknown when the application is written, so it is not possible to include all the connections in the bootstrap script.

Now, what I want to do is, dynamically alter the DB connection that is in the bootstrap or have the ability to dynamically create a new connection for the user signing in. Is there a simple solution for this in Yii and still use AR?
0

#2 User is offline   riccardo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 02-March 10

Posted 03 March 2010 - 10:22 AM

Hi, did someone ask to your question or you found the solution for that?
Because i would like to do something similar...

Thansk,
Riccardo

View PostMerrill, on 02 November 2009 - 04:56 PM, said:

I'm starting a new development.

I have multiple clients (companies) and each client has multiple users.

Each client has their own database, so during user authentication, I discover the name of associated database for that user.

The structure of each database is identical... only the data is different.

The number of clients (and therefor the number of databases) is unknown when the application is written, so it is not possible to include all the connections in the bootstrap script.

Now, what I want to do is, dynamically alter the DB connection that is in the bootstrap or have the ability to dynamically create a new connection for the user signing in. Is there a simple solution for this in Yii and still use AR?

0

#3 User is offline   Merrill 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 26-October 09

Posted 03 March 2010 - 11:21 AM

View Postriccardo, on 03 March 2010 - 10:22 AM, said:

Hi, did someone ask to your question or you found the solution for that?
Because i would like to do something similar...

Thansk,
Riccardo




No, I never received a response. As a result, I decided to use a different framework (QCubed), where there are more users and assistance is more readily available.
0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 03 March 2010 - 03:50 PM

You can override CActiveRecord::getDbConnection like the following:
    public function getDbConnection()
    {
        if(self::$db!==null)
            return self::$db;
        else
        {
            // create DB connection based on your spec here:
            // self::$db=new CDbConnection(...);
            // self::$db->active=true;
            // return self::$db;
        }
    }


Alll your AR classes should then extend this new base class.
0

#5 User is offline   riccardo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 02-March 10

Posted 04 March 2010 - 03:13 AM

Ok, i can extend getDbConnection....but can you give me an example of what i can do?

For example... if i have 4 db connections: db1 , db2 ,db3 and db4.
How can i set dinamically which database connection to use?

Thanks so much

View Postqiang, on 03 March 2010 - 03:50 PM, said:

You can override CActiveRecord::getDbConnection like the following:
    public function getDbConnection()
    {
        if(self::$db!==null)
            return self::$db;
        else
        {
            // create DB connection based on your spec here:
            // self::$db=new CDbConnection(...);
            // self::$db->active=true;
            // return self::$db;
        }
    }


Alll your AR classes should then extend this new base class.

0

#6 User is offline   riccardo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 02-March 10

Posted 04 March 2010 - 04:39 AM

I found this way:


abstract class NewDbConnection extends CActiveRecord
{
private $db_connection='db';

public function getDbConnection()
{
print "--->".$this->db_connection."<br>";
$db = Yii::app()->{$this->db_connection};
$db->setActive(true);
return $db;
}

public function setDbConnection($c)
{
$this->db_connection=$c;
print "X----->".$this->db_connection."<br>";
}

}


It works,
the only issue is that i have to initialize db_connection because getDbConnection is called at model creatin so it need to be a valid database connection that contains the table associated with the model.

Am i correct?

Thanks,
Riccardo

View Postriccardo, on 04 March 2010 - 03:13 AM, said:

Ok, i can extend getDbConnection....but can you give me an example of what i can do?

For example... if i have 4 db connections: db1 , db2 ,db3 and db4.
How can i set dinamically which database connection to use?

Thanks so much

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users