Access module models ?

How can I load the models from a module… I am using the user module but something like: User::model()->findByPK(1);

Inside the application does not work.

Make sure you put the User model inside a directory that you import. i usually put the models under protected/models and add the path alias ‘application.models.*’ to the import array in the application configuration.

Is there a way to autoload module classes? other then adding include paths on all modules?

To use a model from a module, create a path alias to that module and then import the desired model using that path alias, e.g.




Yii::setPathOfAlias('MyAlias','path/to/the/module');


Yii::import('MyAlias.models.DesiredModel');  // this will include the class DesiredModel immediately


$model = Yii::createComponent('DesiredModel');



Make sure your import specifies the path to a particular class (i.e. don’t use a wildcard) so the class will be included immediately.

The class will not included immediately unless you pass in the value ‘true’ to the second argument in the import function to force include the class. other wise it will be included when needed.

Oops, yes you’re right, forgot about that:




Yii::import('MyAlias.models.DesiredClass',true);






Yii::app()->getModule('moduleId');