I have 3 modules in my application. Is there anyway share models between modules. How to call models defined in another modules.Within same module I can do something like User::model() but what if this model is defined in another module. Any suggestion.
Thank you very much that did the job. But I have one question. What if there is model with same name in two different modules. How we can distinguish one from another while referencing it. Is it allowed in YII.
All classes that are to be used at the same time should have unique names.
But I guess we can have different classes with the same name as long as they are module specific and not used at the same time. Supposedly it can be done by (1) refraining from application wide importing and by (2) using CModule::setImport instead.
I have never tested it, and have no guts to try it in my work.
You can import model class from another module by config in init method of module class.
For example, you have an AdminModule. In AdminModule class extends from CWebModule , find init method
at setImport array like that:
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(
'admin.models.*',
'application.modules.module_name.models.*',
'admin.components.*',
));
}
By this way, you just have to import model class from specific module you want.
For more details yii Framework - calling model class from another module
I use another approach. I put all models generated by Gii in protected/models folder. This way I can access to every model from every module. Then, if I want to add some function which is specific for certain module, I extend base model class and put it inside protected/module/adminModule/models.
This way your code is well organised and you can completely change model rules, labels, etc… for every module.
Example:
Base model is class [font="Courier New"]User[/font]:
class User extends CActiveRecord
{
//code genereted by Gii
}
Extended model class saved inside module called administration
class AdministrationUser extends User{
//My specific code
}