Question

This might be a stupid question :) Mage_Customer_Model_Convert_Parser_Customer has the following method:

protected $_collections;

public function getCollection($storeId)
{
    if (!isset($this->_collections[$storeId])) {
        $this->_collections[$storeId] = Mage::getResourceModel('customer/customer_collection');
        $this->_collections[$storeId]->getEntity()->setStore($storeId);
    }
    return $this->_collections[$storeId];
}

I am writing a custom Observer. I emulate the code above as follows:

class Jongosi_Module_Model_Observer
{
    protected $_collections;
    public function getCustomerCollection($storeId = 1)
    {
        if (!isset($this->_collections[$storeId])) {
            $this->_collections[$storeId] = Mage::getResourceModel('customer/customer_collection');
            $this->_collections[$storeId]->getEntity()->setStore($storeId);
        }
        return $this->_collections[$storeId];
    }
}

Obviously, my class doesn't extend anything. I get the following instantiation error:

Call to a member function getEntity() on a non-object in ... on line 8

But the object is instantiated on line 7, yes?

What am I not doing? Thanks.

~ edit ~

The following does work though, unsurprisingly:

class Jongosi_Module_Model_Observer
{
    protected $_collections;
    public function getCustomerCollection($storeId = 1)
    {
        if (!isset($this->_collections[$storeId])) {
            $test = Mage::getResourceModel('customer/customer_collection');
            foreach ($test as $t) {
                var_dump($t->getData());
            }
        }
        return $this->_collections[$storeId];
    }
}

The observer is called by the controller_action_predispatch event in my config.xml file as follows:

...
<frontend>
    <events>
        <controller_action_predispatch>
            <observers>
                <jongosi_module>
                    <class>jongosi_module/observer</class>
                    <method>getCustomerCollection</method>
                </jongosi_module>
            </observers>
        </controller_action_predispatch>
    </events>
</frontend>
...

I am obviously going to change the method that is called, but I was starting with baby steps :) This code is all part of a custom module.

Was it helpful?

Solution

OK, you've got multiple things going wrong here.

First, an observer method has one, and only one argument, the observer object (usually represented by an $observer variable). So your $storeId variable actually has a Varien_Event_Observer object inside of it at the start of your method. Then, your code attempts to use that object as an array key.

PHP doesn't allow objects to be used as array keys. If you have your site in developer mode (in index.php look for setDeveloperMode) you'd get an illegal offset warning. Since you're not in developer mode PHP keeps quiet about the error, but nothing gets set in your array.

All this means $this->_collections[$storeId] returns null, which means

$this->_collections[$storeId]->getEntity()

triggers an error.

Second — that bit of code from Mage_Customer_Model_Convert_Parser_Customer doesn't look like it's current. The getEntity method will return a Mage_Customer_Model_Resource_Customer object — which has no setStore method. So even if you could ignore the above, your code is still going to cause an error.

Third — your observer doesn't DO anything. It returns a collection, but Magento doesn't do anything with return values from observers.

Hopefully that helps point you in the right direction to solve whatever problem it is you're trying to solve.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top