Yii Framework Forum: Module Multiple Instances - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Module Multiple Instances Rate Topic: -----

#1 User is offline   DG Esteban A. Pérez 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 16-September 10
  • Location:Bs. As., Argentina

Posted 26 December 2012 - 09:25 AM

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...
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 26 December 2012 - 11:05 AM

What about "id" property of CModule?
http://www.yiiframew...odule#id-detail
0

#3 User is offline   DG Esteban A. Pérez 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 16-September 10
  • Location:Bs. As., Argentina

Posted 26 December 2012 - 12:11 PM

View Postsoftark, on 26 December 2012 - 11:05 AM, said:

What about "id" property of CModule?
http://www.yiiframew...odule#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 :)
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 26 December 2012 - 01:17 PM

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

#5 User is offline   DG Esteban A. Pérez 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 16-September 10
  • Location:Bs. As., Argentina

Posted 26 December 2012 - 01:36 PM

View Postsoftark, on 26 December 2012 - 01:17 PM, said:

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 :)
0

#6 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 27 December 2012 - 10:57 AM

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.
0

#7 User is offline   DG Esteban A. Pérez 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 16-September 10
  • Location:Bs. As., Argentina

Posted 27 December 2012 - 11:47 AM

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.
0

#8 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 28 December 2012 - 09:54 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users