Module Multiple Instances

Hello,

I need to intantiate several times a module.

After researching, I figured out that itcould be done in this way:




	'modules' => array(

    	'instance_1' => array(

        	'class' => 'application.modules.mymodule.MyModuleModule',

        	'table' => 'table_1',

        	'parameter_1' => 'value_1',

        	...

        	'parameter_N' => 'value_N',

    	),

    	'instance_2' => array(

        	'class' => 'application.modules.mymodule.MyModuleModule',

        	'table' => 'table_2',

        	'parameter_1' => 'value_1',

        	...

        	'parameter_N' => 'value_N',

    	)

    	...



At least i got this: when I use the MyModule in the “menu”: array(‘label’ => ‘Instance_1’, ‘url’ => array(’/instance_1/mymodulecontroller/index’)… I can get it work becouse I can use getController…

However…

If I use one of that instances in this way




Yii::app()->getModule('instance_1')->method;



I know the name of the instance, because in MyModuleController.php, $this refers to MyModuleModule instance and with its ID I can know name and instance configuration.

But in MyModule.php (model) I’ve “lost” this “identification and configuration” ie: table name is on module configuration

So, how I can know the "instance name" in every place of the module: controllers and models, no matter how I instantiate it or get to the module model or module controller.

I already tried, with no success:

  • Use “init()” with getController(), doesn’t work when I use the instatiation as showed above;

  • Use "__construct" with "init()" in the model, when enters in "init()", the variables setted in "__contruct()" are empty;

I need to do it in that way: instantiating the module as many times as I need, configurating the name of the tables (different in each instance) and its messages…

Thanks in advance…

What about "id" property of CModule?

http://www.yiiframework.com/doc/api/1.1/CModule#id-detail

I have no way to know anything about the module instance when I reach the model as I explain before :(

I read several posts about this, and seems no body needs to have several instances of the same module and different configuration for each one.

For example: I developed a "people" manager (module). It has a CRUD part (it is working). Then I need to retrieve the list of "customers", I instatiate the "people module" in this way:




$customers = Yii::app()->getModule('customer')->listPeople;



In PeopleModule I have a method which calls "listPeople" in PeopleController.php, but when I need to "initiate" People model, where I need to retrieve the configuration of this instance of PeopleModule (customer), in the model I have no way to know the instance.

I have access in the controller, but not in the model where I need to know the name of the tables to use in this instance and this information is in the module configuration.

:(

Thank you, any way :)

I think you can retrieve the current module by ‘Yii::app()->controller->module’.

In the case I mentionated, when you use the form Yii::app()->getModule(‘instance’)->action the controller is “SiteController” and it has not any reference to the module… :(

Another idea?

Thanks :)

Dear Friend

The problem here is that when we are creating multiple instances of modules from a single class,

we have to define model class dynamically in accordance with properties declared in module instantiation.

In this scenario, "$this" inside a model is going to refer a instance of that model or going to represent

a row in the table. So using public properties or public methods are not going to infuence the changes in

the class definition.

Here we can declare static properties inside the model and we can change the behavior of the model

dynamically inside a module by changing these properties.

I have a module:PersonModule.

I have two instances:student and teacher.

main.php




................................

'modules'=>array(

	

	'student'=>array('class'=>'application.modules.person.PersonModule','table'=>'student'),

	'teacher'=>array('class'=>'application.modules.person.PersonModule','table'=>'teacher'),

	),

................................



PersonModule.php




class PersonModule extends CWebModule

{      

        public $table;

	public function init()

	{

		$this->setImport(array(

			'application.modules.person.models.*',

			'application.components.person.components.*',

		));


		if(class_exists("Profile"))

		    Profile::$table=$this->table;

	}


.............................................................

............................................................	

}



protected/modules/person/models/Profile.php




class Profile extends CActiveRecord

{      

        public static $table;//added table as static property of AR model.

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	

	public function tableName()

	{

		return self::$table;

	}

...................................................................



Here I have added table as property of module. I am dynamically passing this value to ActiveRecord Profile.

Depending on the module instance it may use table student or table teacher in database.

I hope I helped a bit.

Regards.

Thanks seenivasan and softark…

I found the solution to my issue.

Perhaps I was a little bit confused where to seek.

In my "controller":




$relations = new ItemRelacion(null, $this);

$leafs = $relations->findAll("itmAncestro = $seed");



$relations is OK, but when go to "findAll" native method I found that Yii goes over here (CActiveRecord):




public static function model($className=__CLASS__)

{

    if(isset(self::$_models[$className]))

        return self::$_models[$className];

    else

    {

        $model=self::$_models[$className]=new $className(null);

        $model->_md=new CActiveRecordMetaData($model);

        $model->attachBehaviors($model->behaviors());

        return $model;

    }

}



The line: $model=self::$_models[$className]=new $className(null); is the "problem".

I don’t know if this is ok or not… So I’ll avoid to use findAll or similar, instead I’ll use “createCommand” if the approach of seenivasan doesn’t work for me.

If there is more solutions, they will be wellcomed…

Thanks a lot!

E.

Dear Friend

I would like to say that in my approach I find no issues in fetching records or saving records.

AR treats the dynamically assigned tables as normally assigned ones.

Regards.