Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use CamelCase class names in Magento models?

Tags:

magento

Let's say you have a class name Space_Module_Model_LongModelName and you want to reference it as:

Mage::getModel('module/longmodelname');

This seems to work in some development environments, but doesn't seem to work in all environments. Probably has to do with a file system case sensitivity setting.

The error that you get in environments where it doesn't work is that the include() for Space/Module/Model/Longmodelname.php failed.

like image 540
kalenjordan Avatar asked Jul 18 '12 17:07

kalenjordan


2 Answers

You have a config-node in your config.xml called global/models/yourpackage in which you save your Prefix for your class models.

When you call Mage::getModel('packagename/classname') Magento fetches this config node e.g. Company_Yourmodule_Models adds a _ and then the classname with uppercase first letter:

Company_Yourmodule_Models_Classname

if you have cAMElcaSe classnames, it is the same way. So let's say your class' name is ClassName then you have to call Mage::getModel('packagename/className') and magento resolves it to: Company_Yourmodule_Models_ClassName

like image 72
Fabian Blechschmidt Avatar answered Nov 07 '22 13:11

Fabian Blechschmidt


Take a look at app/code/core/Mage/Core/Model/Config.php specifically getGroupedClassName(); as you'll notice uc_words is used in the method when building the $className, which will capitalize every other word in the class name string.

So your class name of LongModelName will become Longmodelname for the include.

You could easily extend this to work the way you want but since its such a main piece of Magento's factory generation personally I would not touch it in fear of breaking other 3rd party modules, and live with the non-camel case namespace.

The reason ImportExport works is because it is the module name and not a class name. I've run into the same issue before and as annoying as it is I tend to just keep class names non-Camel cased.

like image 30
B00MER Avatar answered Nov 07 '22 13:11

B00MER