I’m working on trying to learn Yii right now. I really enjoy the ability to create the models/CRUD’s of database tables but one thing I’m unclear of is how to model a remote database. For example, modelling a table on my local MySQL installation is fair easy (just match the table name) but lets say you’ve got a table that’s stored in another server and you want to model that. Is there some way to do this?
Right now we’ve got each remote DB connection set up in the config/main.php file and it seems to function alright when I do all of the modelling by hand. However, it’s pretty time consuming and error prone. If there’s an easier way, I’d love to learn.
Thanks in advance!
I have not tried this myself, but yii uses DSNs to initialize a database connection, so this should work…
Try to copy config/main.php once for each remote server using different names (eg., remote1.php, remote2.php, etc,). The yiic shell command is hard coded to use the ‘db’ database connection. Change the db connection for each config file to reflect the remote database server connection parameters. Then call the appropriate config file when loading the shell from your protected/ directory (e.g, yiic shell config/remote1.php).
For the connectionString parameter in each db connection, you may need to provide the port number (i.e, the port used by the database server) (e.g., ‘connectionString’=>‘mysql:host=localhost;port=<portnumber>;dbname=galena’.
Thanks for the reply cmx - I was thinking something like that myself, but I wasn’t sure if there was a better way. I’ll let you know how it works out when I try it in a few days.
This worked just fine! I was happy with the result, except I tried it in another directory to be safe. I suggest doing it as cmx suggested and staying in the same directory with copies of your config.php file. It’ll make the model generation a lot easier.