Use model of module

Hi folks,

I'd like to know if I always have to do add following lines in the controller of a module, when I want to use the model of a module?

Yii::import('application.modules.myModule.*');

require_once('models/MyModel.php');

If these lines are not in the controller class, I always get an error. Are the model classes not included automatically?

Greetings

Dan

EDIT:

Ok, I've found this solution here in the forum:

http://www.yiiframew…34.html#msg6334

Is this the only/right solution for my problem, or just a workaround?

You can use

Yii::import('moduleID.models.*');

If the models are only used within the module, you can put the above line in the init() method of your module class.

If the models may also be used outside of the module, then you need to put it in the app config.

what if I want to load models (to be used outside of the module) dynamically (read from database for available models) ?

Did you get it???

How to get models info outside an Module??????

in config/main.php there are lines :


// autoloading model and component classes

	'import'=>array(

		'application.models.*',

		'application.components.*',

		'application.modules.sysModule.models.*',

		'application.modules.sysUser.models.*',

		'application.modules.sysGroup.models.*'

	),

I’ve added 3 more models (sysModule, sysUser and sysGroup). In the future, if I develop another module, then I have to manually add the newly-developed-module’s-model to that main.php.

is there a way that the model’s path is stored inside a table in database?, then the table is loaded to fill the content of import array ? , so that when the application runs, I can use any models (from any modules).

Since the config file is PHP, you can do a lot of things to solve this dynamic import problem. ;)

I use this to dinamically add modules…

here’s the full main.php




<?php


$config = array(

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'google.com',


	'preload'=>array('log'),


	'import'=>array(

		'application.models.*',

		'application.components.*'

	),


	'components'=>array(

		'log'=>array(

			'class'=>'CLogRouter',

			'routes'=>array(

				array(

					'class'=>'CFileLogRoute',

					'levels'=>'error, warning',

				),

			),

		),

		'user'=>array(

			'allowAutoLogin'=>true,

		),

		

		'db'=>array(

			'class'=>'CDbConnection',

			'connectionString' => 'mssql:host=mahost;dbname=madb', 'username'=>'sa', 'password'=>'mapwd'

		)

	),


	'params'=>array(

		'adminEmail'=>'admin@google.com',

	),

	

	'modules'=>array(),

	

	'theme'=>'classic'

);


// Get connected to DB

$connection=new CDbConnection(

	$config['components']['db']['connectionString'],

	$config['components']['db']['username'],

	$config['components']['db']['password']

);


$connection->setActive(true);


// Get active modules from DB

$command=$connection->createCommand("SELECT * FROM sys_modules WHERE m_state = 1");

$reader=$command->query();


// Parse to variables

foreach($reader as $row){

	$modules[$row['m_name']] = array();

	$imports[] = 'application.modules.'.$row['m_name'].'.models.*';

	$imports[] = 'application.modules.'.$row['m_name'].'.components.*';

}


// Combine with config

$config['import']  = array_merge($config['import'],$imports);

$config['modules'] = array_merge($config['modules'],$modules);


$connection->setActive(false);


// Send it to application

return $config;

I think this is not the best (optimal) way… any suggestion?