kalenjordan
kalenjordan

Reputation: 2446

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

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.

Upvotes: 7

Views: 5205

Answers (4)

SteveFromAccounting
SteveFromAccounting

Reputation: 197

Four years later, another note:

If you want to use camelCased classnames in addition to filenames, make sure the following part of your {moduleName}/etc/config.xml is camelCased as well:

```xml

<global>
    <models>
        <ProbablyYourCompanyOrModuleName>
            <ModuleName_resource>
                <entities>
                    <!-- "longModelName" below should be CamelCased, but prob the first letter will be lowercased -->
                    <longModelName>
                        <table>your_db_table_name</table>
                    </longModelName>
                </entities>
            </ModuleName_resource>
        </ProbablyYourCompanyOrModuleName>
    </models>
</global>

```

The portion should be camelCased with the first letter lowercased. Otherwise, you'll get an exception of Can't retrieve entity config: yourmodule/longModelName

(see Mage_Core_Model_Resource::getTableName())

Upvotes: 0

B00MER
B00MER

Reputation: 5491

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.

Upvotes: 2

Fabian Blechschmidt
Fabian Blechschmidt

Reputation: 4141

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

Upvotes: 17

Jerzy Zawadzki
Jerzy Zawadzki

Reputation: 1985

For sure there is possibility to use camelcase - you can see module e.g. ImportExport in Magento core. I think all is about setting correct (with camel cases) names in xmls

Upvotes: 1

Related Questions