[SOLVED] Gii generates a model for a module with a different database

Dear Guys,

The question may make you confuse here is an explanation.

I have a module which I intend to use another database for my project.

So I set my init() like this in MyModuleModule.php




	public function init()

	{

		// this method is called when the module is being created

		// you may place code here to customize the module or the application


		// import the module-level models and components

		$this->setImport(array(

			'mymodule.models.*',

			'mymodule.components.*',

		));

		

		$this->setComponents(array(

			'db' => array(

				'connectionString' => 'mysql:host=localhost;dbname=myanotherdb',

				'username' => 'root',

				'password' => '',

				'charset' => 'utf8',

			),

		));

	}



It is what I believe it’ll alter the database connection for this module.

That means when generating a model for this module. Gii has to use this DB setting for its connection.

But it isn’t. Gii still connects to the database of the main config.

I generate model for this model using the same way as I generate for my application.

Well, I have to make a mistake somewhere. More over I’ve used yii-environment extension you can see here. Don’t know if this has to do something with my problem.

So do you guys have any idea what is wrong? Thanks a lot.

check this extension I wrote a while ago

Cool! Thanks, Gonna try it today.

Alright, it works as I intended. This is just fine and Ok for me.

But for programming perspective, I would rather have those setting lies in Module configuration itself. Since it would be a self contained module that can re-use any where.

By doing this solution, I can’t just copy + paste my module to some other software. I have to install the extension again which is not a big deal.

Just sayin’ that it would be nice to be like what I’ve said.

Anyway thanks again.

Solution

  1. Install Gustavo’s MultiActiveRecord extension, first download it.

  2. Copy "MultiActiveRecord.php" to your main components folder.

  3. Configure Gii to call "ext.mpgii"

  4. Configure additional DB in main config file.

  5. Using "MPGii" to generate model for the module.

  6. Use "MultiActiveRecord" as based class.

  7. Use that additional database as connection id.

So if we move this module to other software we have to install Gustavo’s MultiActiveRecord again.

=== End of Line ===