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?



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




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


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

You can use


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








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


$config = array(














					'levels'=>'error, warning',










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











// Get connected to DB

$connection=new CDbConnection(






// Get active modules from DB

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


// 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);


// Send it to application

return $config;

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