Calling Model of another Modules

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.

Configure your config/main.php like the following:




	'import'=>array(

		'application.models.*',

		'application.components.*',

		'zii.widgets.*',

		'zii.widgets.jui.*',

		...

		'application.modules.module_a.models.*',	// module_a

		'application.modules.module_a.components.*',

		'application.modules.module_b.models.*',	// module_b

		'application.modules.module_b.components.*',

	),



Then you will be able to access the models and the components in those modules from anywhere in the app. :)

Hello

@nicholasnet

If you have a relation between all your Model than also easily handle your problem.

for example you have 2 models

User (id , name)

Department (id, user_id, name) (user_id) ref from user table

Add this line in User model in relation array(‘dept’, self:HAS_MANY, ‘Department’, user_id)

Now you want all department name than just you have to call like




$user = User::Model()->findAll();

foreach($user as $data){

  foreach($data->dept as $depart){

    echo $data->name.'==='.$depart->name;

  }

}

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.

It is not.

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. :rolleyes:

a bit late, may be helpful for some one.

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

Is it applied also in yii2?? I tryied this in yii2 with no success

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


}