Magento code/local case sensitivness
I found an odd behaviour, and I need some help if someone knows the answers.
Behaviour seems different between app/code/local/Mage and app/code/local/MyCompany (or whatever)
If I have a module Mymodule in app/code/local/Mage wit开发者_开发问答h a Model Mymodel (app/code/local/Mage/Mymodule/Model/Mymodel) I can load it with :
Mage::getModel('*M*ymodule/Mymodel') or Mage::getModel('*m*ymodule/Mymodel') both are working ...
But If the module is in app/code/local/Mycompany the name is case sensitive and only one will work, depending on config.xml
<model>
<**m**ymodule>
<class>Mycompany_Mymodule_Model</class>
<resourceModel>cybermut_mysql4</resourceModel>
</**m**ymodule>
</models>
will allows Mage::getModel('*m*ymodule/Mymodel')
and
<models>
<**M**ymodule>
<class>Mycompany_Mymodule_Model</class>
<resourceModel>cybermut_mysql4</resourceModel>
</**M**ymodule>
</models>
will allows Mage::getModel('*M*ymodule/Mymodel')
Why is there this different behaviour between local/Mage and local/Alltheothers ???
PS: It's not a rewriting it's a home made module and I'm using CE 1.4.2 Best Regards
The difference is that when your module is under Mage/Mymodule with a shortname of mymodule and you call Mage::getModel('Mymodule/mymodel') you end up hitting the fall-back behavior.
If the resolver does not find an entry for the shortname Mymodule (case-sensitive), then it defaults to a classname of mage_mymodule_model_mymodel and since your module is under the Mage namespace that works.
By moving your module into a company specific namespace you end up with a classname that does not work if it hit the fall-back. You need mycompany_mymodule_model_mymodel and instead end up with mage_mymodule_model_mymodel.
The relevant code is in Mage_Core_Model_Config::getGroupedClassName()
public function getGroupedClassName($groupType, $classId, $groupRootNode=null)
{
if (empty($groupRootNode)) {
$groupRootNode = 'global/'.$groupType.'s';
}
$classArr = explode('/', trim($classId));
$group = $classArr[0];
$class = !empty($classArr[1]) ? $classArr[1] : null;
if (isset($this->_classNameCache[$groupRootNode][$group][$class])) {
return $this->_classNameCache[$groupRootNode][$group][$class];
}
//$config = $this->getNode($groupRootNode.'/'.$group);
$config = $this->_xml->global->{$groupType.'s'}->{$group};
if (isset($config->rewrite->$class)) {
$className = (string)$config->rewrite->$class;
} else {
if (!empty($config)) {
$className = $config->getClassName();
}
if (empty($className)) {
$className = 'mage_'.$group.'_'.$groupType;
}
if (!empty($class)) {
$className .= '_'.$class;
}
$className = uc_words($className);
}
$this->_classNameCache[$groupRootNode][$group][$class] = $className;
return $className;
}
NB: Magento calls uc_words on $classname.
function uc_words($str, $destSep='_', $srcSep='_')
{
return str_replace(' ', $destSep, ucwords(str_replace($srcSep, ' ', $str)));
}
精彩评论