Access module function

I have some functions in MyModule class for serving data for i.e. listData but they are all not static.

So if I do the following:

echo CHtml::activeDropDownList($mymodel, 'strVar', CHtml::listData(MyModule::getListData(), 'strVar', 'strValue'));

I get an error:

Property "MyModule.arrFilter" is not defined.

the function in MyModule does the following:

public function getListData()

{

  return $this->getList('my/var/', 'strVar', $this->arrFilter['strVar']);

}

It seems like the function and the members must be static, right?

Is the MyModule class the right place to have this functions, or should they be somewhere else?

Did you define arrFilter in your module class?

Also, you are calling getListData in a static way. You can get the current module via $this->module, where $this is the controller.

Quote

Did you define arrFilter in your module class?

Also, you are calling getListData in a static way. You can get the current module via $this->module, where $this is the controller.

Yes, I defined arrFilter like this:

public $arrFilter;

and from the controller class the function getListData is callable without errors. But I don't want to call the function (or better functions, there are 20 and more) through the controller class, and if I want to call it in a static way, i get the error (which I wrote above).

The key is that you shouldn't call the method in a static way if the method is referencing $this.

Quote

The key is that you shouldn't call the method in a static way if the method is referencing $this.

Ok, I thought the same. But how can I call a method of the module in a view file?

EDIT:

Ok, now I've got it doing like this:

In controller:

$this->render('search',array('mymodule' => $this->module));

In the view file:

CHtml::listData($mymodule->getListData(), 'strVar', 'strValue');

and it works. is this the way it should be?

There's not strict way for this kind of problem. What you did is definitely fine. You may also put these shared methods in a separate class, or scatter them into model classes.

bitmatix

Do you know about any full working module for sample?