How to load specific model for a module from a config file for a module ?

Dear all,

I want to use a model in my controller. But if I instantiate [font=courier]CatwalkModel[/font] from my controller let's say [font=courier]IndexController[/font], it kept getting error that the [font=courier]CatwalkModel[/font] is not found:

Here is the error message I got:

Quote

Fatal error: Class 'CatwalkModel' not found in .... blah blah blah

How can I import the models that I stored in a [font=courier]modules/amodule/models[/font] folder from the [font=courier]modules/amodule/config/main.php[/font] folder ?

So here is the background why I am now meet this problem.

I have created a module, let's say module "A" that has loads a config file:

Within my module folder, I have created a config folder to store the main.php as the specific module configuration file. ([font=courier]application/modules/amodule/config/main.php[/font]):

Next, I developed a model namely [font=courier]CatwalkModel[/font] and stored it in the [font=courier]modules/amodule/models/CatwalkModel.php[/font]:

Thanks before, and after!

Is IndexController part of the module?

Oh yes, sorry to not mention it, the IndexController is in the modules/amodule/controllers.

hmm…I can't find anything wrong here. Your code should work. Could you please send me the complete code that can reproduce this issue? Thanks.

Sure, in order to set this quickly, your structure:

[font=courier]

www

  yourwebname

      application  --> put my file here.

      framework

      requirements[/font]

Okay, to access the url I meant, type this in your URL, where [YOURWEBNAME] should be replaced by any of your choice:

[font=courier]http://localhost/[YOURWEBNAME]/application/index.php/authenticate/index/login[/font]

Note:

Right now, I also set the index.php to be hidden by providing the .htaccess file, and also set up the showScriptName to false in the [font=courier]application/config/main.php[/font]. Therefore, if you access from your local server like stated below, you will get an error saying that the page was not found (which was also in my plan to ask about this later actually, because it should work, but instead I got error saying page not found ??):

[font=courier]http://localhost/you…enticate/index/[/font]

Thanks!

So, any news ? I still can't figure out why…

ok, found the problem: configure() method expects an array instead of the path to the configuration file.

Quote

ok, found the problem: configure() method expects an array instead of the path to the configuration file.

Hi qiang! Thank you, so you're saying that I need to open the main.php manually using plain PHP ?

I was trying to play around, and found that Yii also has the CConfiguration class (http://www.yiiframework.com/doc/api/CConfiguration).

But, when I apply to a module, why can't a module accept object in its init method as a parameter ? It would be nice if it can do that.

Thanks!

Quote

Quote

ok, found the problem: configure() method expects an array instead of the path to the configuration file.

Hi qiang! Thank you, so you're saying that I need to open the main.php manually using plain PHP ?

I was trying to play around, and found that Yii also has the CConfiguration class (http://www.yiiframework.com/doc/api/CConfiguration).

But, when I apply to a module, why can't a module accept object in its init method as a parameter ? It would be nice if it can do that.

Thanks!

Ok, after playing for more, here is how to load separated config file to a module:

Yii is just damn cool!

you just need $this->configure(include($configFile));

Quote

you just need $this->configure(include($configFile));

Which would you suggest then ? What do you think, if I use the [size=2][font=courier]$config->applyTo() [/font][/size]?

Thank you!

Using applyTo is also fine, but it introduces an additional unnecessary layer of complexity. So I suggest using include() directly here.

Quote

Using applyTo is also fine, but it introduces an additional unnecessary layer of complexity. So I suggest using include() directly here.

Thank you, Shi-Fu! ;)