Generate Model With Shell

Hello friends

I have 2 databases on my application, in my main.php code I do this:


			'connectionString' => 'mysql:host=;dbname=gestao',

			'emulatePrepare' => true,

			'username' => 'root',

			'password' => '123456',

			'charset' => 'utf8',


	    'db2' => array(

	        'connectionString' => 'mysql:host=;dbname=portal',

	        'username'         => 'root',

	        'password'         => '123456',

	        'charset'          => 'utf8',

	        'class'            => 'CDbConnection',          // DO NOT FORGET THIS!


When I need generate a new model on application that have only one database, I access the console and when I access shell I type this command:

>> model MyModel mymodel

But now I need create a new model on my second database. How can I do this?

I think gii allows you to select the db connection. Also search for ‘using 2 databases in yii’ I have seen a number of tutorials about how to do it.

Yes. There area tutorials about this, but using gii, I would like to use shell to do this, but I did not found.


Not sure why you need to use the shell, but I think you need to change the db settings in the console config file run the yiic, then change back. I seem to remember someone showing how to use two different db. You would set ‘components’=>array(‘db’=>…, ‘db2’=>…) in the config file. The shell command would only use $db, not $db2. So you had to change the ‘db2’=> to ‘db’=> run the shell, then change back. There might be some function in the generated model that you had to overwrite to tell Yii which db to use to get the correct table.