0

I have 2 databases.

db1 and db2. I defined them in config/main.php file

db1 has table table1 and db2 has table2

table1 has a column which is primary key of table2

How do I define relations in table1?

I did it as usual but it is retrieving data from db1.table2.

Basically I want to link db1.table1 and db2.table2

1
  • Hi user, if I post my complete code to make this work. Would you give me the tick? Only than I will help ya. :) I solved this already.
    – lin
    Nov 17, 2014 at 8:30

2 Answers 2

0

You have to use Yii Query Builder, and make a query like this:

SELECT u1.* FROM database1.users u1 LEFT JOIN database2.users u2 ON u1.id=u2.id WHERE u2.id IS NOT NULL;
0

To use your another db connection, You can override getDbConnection() method in your model. Take a look:

public function getDbConnection(){
    Yii::app()->db2->setActive(true);
    return Yii::app()->db2;
}

Anyway, Yii 1.X does not officially supports Cross-Database relations. You may need to write some codes. As Yii's official Wiki says:

Limitations on Multi-DB support: Tables in one database cannot directly reference tables in another database, and this means that relations don't cross DB boundaries.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.