1

I keep getting the error mentioned in the title when trying to submit a form that contains a customer message. It is a form that when submitted is supposed to save the info in the database. Here is the function

public function formPostAction()
{
    if (!$this->_validateFormKey()) {
        return $this->_redirect('*/*/');
    }
    // Save data
    if ($this->getRequest()->isPost()) {
        $data = $this->getRequest()->getPost();
        $customerId = $this->_getSession()->getCustomer()->getId();

        $message = Mage::getModel('hwcustomer/message')
            ->setData($data)
            ->setMessage($data['message'])
            ->setSubject($data['subject'])
            ->setParentId($customerId);

        $messageId = $this->getRequest()->getParam('id');

        $message->setId(null);
        if ($messageId) {
            $customerMessage = Mage::getModel('hwcustomer/message')->load($messageId);
            if ($customerMessage->getId() && $customerMessage->getCustomerId() == $this->_getSession()->getCustomerId()) {
                $message->setId($messageId);
            }
        }
        try {
            $messageValidation = $message->validateMessage($data);
            if (empty($messageValidation)) {
                $message->save();
                $this->_getSession()->addSuccess($this->__('The message was successfully saved'));
                $this->_redirectSuccess(Mage::getUrl('customer/messages', array('_secure'=>true)));

                return;
            } else {
                if (is_array($messageValidation)) {
                    foreach ($messageValidation as $errorMessage) {
                        $this->_getSession()->addError($errorMessage);
                    }
                } else {
                    $this->_getSession()->addError($this->__('Can\'t save message'));
                }
            }
        }
    }
}
5

You haven't mentioned which line the error occurs on, so I assume it's here:

$message = Mage::getModel('hwcustomer/message')
    ->setData($data)                // <------------------- this line?
    ->setMessage($data['message'])
    ->setSubject($data['subject'])
    ->setParentId($customerId);

What does this mean?

It means that your call to retrieve the model with the alias hwcustomer/message failed to resolve to a class name. Mage_Core_Model_Config::getModelInstance() returns false if the class doesn't exist after it's alias is resolved.

How can I identify the problem?

Firstly, check that hwcustomer/message is what you intended to write. If there's an obvious typo, stop reading now and fix it.

Using n98-magerun is a handy way to check if your model alias resolves correctly from the command line:

n98-magerun.phar dev:class:lookup model hwcustomer/message

If everything is OK, the above command should show your custom class name. If the class didn't resolve correctly or it doesn't exist, it will show an incorrect class name and/or that it doesn't exist.

Check the model exists

Firstly, you have to find where the model alias is defined. It's likely that this is a custom module that you've written (?) so you should know. If not, you need to find the module that defines something like this in its config.xml file:

<models>
    <hwcustomer>
        <class>Vendor_Module_Model</class>
    </hwcustomer>
</models>

Once you've found that:

  • The <hwcustomer> tag is the first part of your model alias
  • The <class> tag will tell you where to look for a class named Message. Let's assume for argument's sake that your <class> tag says <class>Me_Hwcustomer_Model</class>

You'll either look for the model in app/code/local/Me/Hwcustomer/Model, or community if it's in the community pool.

Your model should exist and look something like this:

# File: app/code/local/Me/Hwcustomer/Model/Message.php
class Me_Hwcustomer_Model_Message extends Mage_Core_Model_Abstract
{

}

In the above example the Message part of the filename is what is lowercased and used for the second half of the model alias hwcustomer/message.


How do I fix it?

Ensure that all of the above configuration settings, file paths and file names check out correctly.

Other things to check

  • Ensure app/etc/modules/Me_Hwcustomer.xml (probably not called this, just an example) exists and the <codePool> tag is correct (either local or community)
  • Ensure that wherever the module's <name> tag is used in XML, it is spelled correctly and that there are no upper or lowercased letters where there shouldn't be
  • Ensure that you have cleared your cache. XML configuration is cached by default in Magento to reduce load time. If you change XML configuration values, you need to clear your cache.

Validating that the problem is fixed

Once you've found the problem and fixed it, you can test it quickly using n98-magerun again (the same command as provided earlier), and/or by running your process again.

  • 1
    You are exactly right. It was supposed to be 'hwcustomer/messages' instead of 'hwcustomer/message' – djames Jun 10 '16 at 3:26
  • @djames Great! You've got the same model call a few lines below too. – Robbie Averill Jun 10 '16 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.