Multiple Databases Dao Sql In Yii 2.0
#1
Posted 02 February 2013 - 05:16 PM
Active Record works fine across multiple databases, because each model can be linked to an individual DB.
But in DAO SQL, it seems that you are limited to one connection: $connection=Yii::app()->db;
Do I have it wrong? Or, is this something that could be addressed in Yii 2.0?
Many thanx
#2
Posted 02 February 2013 - 05:29 PM
In yii2 AR relations over multiple dbs will work better as no join is used.
#3
Posted 03 February 2013 - 05:04 AM
Yes, I hope I am wrong.
Can you please show a simple DAO SQL example using two tables in different databases.
#4
Posted 03 February 2013 - 05:33 AM
CeBe, on 02 February 2013 - 05:29 PM, said:
Hi CeBe,
Do you mean that relational AR in yii2 will be something very different from that of yii1?
Or will it still join the related tables when we specify "with" in our criteria?
#5
Posted 03 February 2013 - 03:45 PM
Quote
Or will it still join the related tables when we specify "with" in our criteria?
Not entirely different but different enough. It still can join related tables, I think.
Enjoying Yii? Star us at github
Support me so I can work more on Yii: https://www.patreon.com/samdark
#6
Posted 03 February 2013 - 08:35 PM
samdark, on 03 February 2013 - 03:45 PM, said:
Sounds exciting enough.
I'm sure yii2 will have a better strategy for relational AR and the things around eager loading, lazy loading, "with" and "together" will be more straight and easy.
Thank you for your work, dev team.

#7
Posted 04 February 2013 - 03:33 AM
But I still need to know how to do a single DAO SQL statement using two tables in different databases.
#8
Posted 04 February 2013 - 04:11 AM
In MySQL you can prefix the tablename with db name to access tables from different dbs.
#9
Posted 04 February 2013 - 06:14 AM
How do you do that?
I mean, $connection=Yii::app()->db only connects to a single db at a time.
I'm using MySQL.
#10
Posted 04 February 2013 - 06:52 AM
Enjoying Yii? Star us at github
Support me so I can work more on Yii: https://www.patreon.com/samdark
#11
Posted 04 February 2013 - 06:53 AM
#12
Posted 04 February 2013 - 06:55 AM
EDIT: Too late....

#13
Posted 04 February 2013 - 07:44 AM
I already incorporated everything in that wiki into my system. It works great - in AR. But I can't see anything in the wiki about using sql.
I have monster size reports that I must convert to Yii. They incorporate data from various tables and some of the sql statements are half a page long. I guess this could be difficult with AR, so I would prefer to keep it in sql.
CeBe and Haensel, this is news to me. I thought $connection1=Yii::app()->db1 connects to a database and $connection2=Yii::app()->db2 connects to a different database, regardless of whether thay are both in the same RDMS or not. Do I understand you correctly that I only need one connection for two separate databases that run on the same MySQL instance?
What about running the databases on different machines? I suppose you need two connections then. How will the sql in Yii look for two connections?
Note that the wiki Samdark was referring to, uses 2 connection strings for the different databases. Are we saying that 1 connection string is enough?
#14
Posted 04 February 2013 - 02:13 PM
#16
Posted 06 February 2013 - 01:18 PM
Gerhard Liebenberg, on 04 February 2013 - 07:44 AM, said:
That is correct. As long as theyre on the same machine, you only need a single connection to that machine, even if that machine has multiple databases configured.
There's an example in the comments of that wiki page:
http://www.yiiframew...t-in-yii/#c4816
#17
Posted 06 February 2013 - 02:39 PM
I would think that, splitting your tables into multiple databases, only to have them located on the same machine, sort of defeats the purpose - doesn't it? I mean, the only good reason I can think of for having multiple databases, is to have them on DIFFERENT machines. Otherwise, what on earth is the purpose of doing it in the first place?
yJeroen, thank you for your reply. It confirms.
However, it seems that the example you refer to in the wiki works with AR, and not with DAO sql - which is exactly my problem. (I might be wrong here, but it looks like it only pertains to AR models.)
So, I guess for databases on different machines, you will have to run separate sql queries; and then merge them afterwords.
#18
Posted 06 February 2013 - 03:11 PM
CeBe already gave you an example how to do it with DAO. Because that's basically just using custom SQL. However, like explained earlier that can only be done if the multiple databases are on the same machine; so you can use a single connection to acces multiple databases.
#19
Posted 06 February 2013 - 03:31 PM
But what about DAO for databases on different machines? I suppose we will have to run separate sql queries via separate connections; and then merge them afterwords?
#20
Posted 06 February 2013 - 03:35 PM