mage registry key “_singleton/” already exists

2019-07-27 18:53发布

问题:

I know there are lot of posts with this problem, but I guess each of them is with different roots of it (at least from what I checked - nothing helped me).

I am trying to fire the event upon click on a button from the user, but I get the upper mentioned exception in a browser alert Mage registry key "_singleton/" already exists.

The part of the config.xml:

.....
     <models>
        <packagecustomernumber>
            <class>Package_CustomerNumber_Model</class>
        </packagecustomernumber>
    </models>

</global>
<frontend>
        <events>
            <checkout_type_onepage_save_order>
                <observers>
                    <type>singleton</type>
                    <class>packageName/customernumber/observer</class>
                    <method>setCustomerNumber</method>
                </observers>
            </checkout_type_onepage_save_order>
        </events>
    </frontend>

And the class itself:

class Package_CustomerNumber_Model_Observer
{
    public function setCutomerNumber($observer)
    {
        die('setCutomerNumber');
    }
}

The button which should fire the even it checking out/saving the order, so the event should be correct.

Any suggestions ?

回答1:

The first thing that pops out is this

<class>packageName/customernumber/observer</class>

That's invalid. This is the node where you're telling Magento what class to use for your observer. As such, the <class/> node should be either the full PHP class name of your observer

<class>Package_CustomerNumber_Model_Observer</class>

Or a class aliases for the model

<class>packagecustomernumber/observer</class>

Also, before running your observer, it helps to make sure you can instantiate your model class. Try running the following code in a Magento loaded environment (script, controller action, phtml template, etc.)

$model = new Package_CustomerNumber_Model_Observer;
var_dump(get_class($model));

$model = Mage::getModel('packagecustomernumber/observer');
var_dump(get_class($model));

If you can't instantiate the class, then Magento won't be able to either (and it's easier to test this first before running through some steps to trigger your observer).



回答2:

Yes, the "packageName/customernumber/observer" is the source of the problem.

while this class reference is completely incorrect in its structure, the problem actually comes up when your class reference does not match up with your global/models/modulename definition. even when the reference "looks" correct.

The config :

<config>
<global>
    <models>
        <mymodule>
            <class>My_Module_Model</class>
        </mymodule>
    </models>
    <events>
        <some_event_tag>
            <observers>
                <my_event_observer_method>
                    <class>my_module/observer</class>
                    <method>myEventObserverMethod</method>
                </my_event_observer_method>
            </observers>
        </some_event_tag>
    </events>
</global>
</config>

Will have the same result because "my_module/observer" is not found, since the "my_module" class group node is not configured. The correct use for this sample would have been "mymodule/observer".

So if you run across this error, re-read your config.xml.



回答3:

Make sure that your config.xml models section contains

        <!-- This says that string 'company_module' corresponds to Company_Module_Model pseudo-namespace in getModel() and getSingleton() calls. -->
        <company_module>
            <class>Company_Module_Model</class>
        </company_module>

Otherwise you won't be able to make new model instance.



标签: magento