cancel
Showing results for 
Search instead for 
Did you mean: 

Observer with no effect

Observer with no effect

Hi!

 

  1. Login to the website frontend in a customer account.

    Click on "Account information" and edit some information (firstname, lastname, ...).
    Click on the save button. Now login to the magento backend and click on customers -> manage customers.
    Now you see the grid with the correct "updated_at" timestamp.

 

  1. Login to the website frontend again. Click on "Account information"

    And now click on "Addressbook" and edit some information.
    Click on the save button. Now go to the magento backend and click on customers -> manage customers.
    Updated_at is not changed.

 

I create a modul to update the updated_at field. But i see no effect when i click the SAVE button in the customer.

My files:

/app/etc/modules/Easylife_Meta.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Meta>
            <codePool>local</codePool>
            <active>true</active>
        </Easylife_Meta>
    </modules>
</config>

 

/app/code/local/Easylife/Meta/Model/Observer.php

<?php
class Easylife_Meta_Model_Observer 
{
public function autoMetaDescription(Varien_Event_Observer $observe)
{
$_address = $observer->getEvent()->getCustomerAddress();
}
if (!$_address->hasDataChanges())
{
return $this;
}
$_customer = $_address->getCustomer();
$_customer->setUpdatedAt(Varien_Date::now())
->save();
}
?>

/dev-brand-top/app/code/local/Easylife/Meta/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
<Easylife_Meta>
<version>0.0.1</version>
</Easylife_Meta>
</modules>
<global>
<models>
<easylife_meta>
<class>Easylife_Meta_Model</class>
</easylife_meta>
</models>
</global>
<frontend>
<events>
<customer_address_save_after><!-- observe the event -->
<observers>
<easylife_meta>
<class>easylife_meta/observer</class>
<method>autoMetaDescription</method>
</easylife_meta>
</observers>
</customer_address_save_after>
</events>
</frontend>
</config>

 

What is wrong? Thank you.

Thomas

4 REPLIES 4

Re: Observer with no effect

Hi @Philosoph83

 

I think you have the right idea, the problem is that Magento required models to have specified an $_eventPrefix in the model to trigger a specific event for you to listen on.

 

See the follow code from Mage_Core_Model_Abstract

 

 

    /**
     * Processing object after save data
     *
     * @return Mage_Core_Model_Abstract
     */
    protected function _afterSave()
    {
        $this->cleanModelCache();
        Mage::dispatchEvent('model_save_after', array('object'=>$this));
        Mage::dispatchEvent($this->_eventPrefix.'_save_after', $this->_getEventData());
        return $this;
    }

 

You can use model_save_after event and the verifying what model is being updated.

 

Due note that this will trigger on every singel model saved, so if you can avoid using this it would be better.

 

I hope this helps :-)

 

Re: Observer with no effect

Hi!

 

Thank you for your post! I change the code now.

But i get an error massage.

My new code:

<?php
class Easylife_Meta_Model_Observer 
{
    public function autoMetaDescription(Varien_Event_Observer $observer)
    {
        $address = $observer->getCustomerAddress();
        if (!$address->hasDataChanges()) {
            return $this;
        }

        $customer = $address->getCustomer();
        $_customer = Mage::getModel('customer/customer')->load($customer->getId());
        $_customer->setUpdatedAt(now())->save();
    }
}
?>

 

When i click the SAVE button the site is loading for 1 or 2 minutes and then i get this magento error:



There has been an error processing your request
SQLSTATE[HY000]: General error: 2006 MySQL server has gone away, query was: SELECT `main_table`.* FROM `design_change` AS `main_table` WHERE (store_id = :store_id) AND (date_from <= :required_date or date_from IS NULL) AND (date_to >= :required_date or date_to IS NULL)

Trace:
#0 /homepages/16/d280867927/htdocs/dev-brand-top/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#1 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#2 /homepages/16/d280867927/htdocs/dev-brand-top/lib/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#3 /homepages/16/d280867927/htdocs/dev-brand-top/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT `main_ta...', Array)
#4 /homepages/16/d280867927/htdocs/dev-brand-top/lib/Varien/Db/Adapter/Pdo/Mysql.php(504): Zend_Db_Adapter_Pdo_Abstract->query('SELECT `main_ta...', Array)
#5 /homepages/16/d280867927/htdocs/dev-brand-top/lib/Zend/Db/Adapter/Abstract.php(756): Varien_Db_Adapter_Pdo_Mysql->query(Object(Varien_Db_Select), Array)
#6 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Model/Resource/Design.php(179): Zend_Db_Adapter_Abstract->fetchRow(Object(Varien_Db_Select), Array)
#7 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Model/Design.php(62): Mage_Core_Model_Resource_Design->loadChange('1', NULL)
#8 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Model/App/Area.php(162): Mage_Core_Model_Design->loadChange('1')
#9 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Model/App/Area.php(124): Mage_Core_Model_App_Area->_initDesign()
#10 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Model/App/Area.php(92): Mage_Core_Model_App_Area->_loadPart('design')
#11 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Model/App.php(789): Mage_Core_Model_App_Area->load()
#12 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Controller/Varien/Action.php(512): Mage_Core_Model_App->loadArea('frontend')
#13 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Controller/Front/Action.php(69): Mage_Core_Controller_Varien_Action->preDispatch()
#14 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Customer/controllers/AddressController.php(49): Mage_Core_Controller_Front_Action->preDispatch()
#15 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Controller/Varien/Action.php(407): Mage_Customer_AddressController->preDispatch()
#16 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('formPost')
#17 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#18 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Model/App.php(365): Mage_Core_Controller_Varien_Front->dispatch()
#19 /homepages/16/d280867927/htdocs/dev-brand-top/app/Mage.php(684): Mage_Core_Model_App->run(Array)
#20 /homepages/16/d280867927/htdocs/dev-brand-top/index.php(83): Mage::run('', 'store')
#21 {main}

Re: Observer with no effect

Hi @Philosoph83

 

You are running into the issue that this will create an infinite loop since addresses are saved after the customer model us saved. So the code you have written will keep triggering itself.

 

Try looking into using the saveAttribute method instead, its located on the resource customer model.

Re: Observer with no effect

@Theis Corfixen

 

Thank you for your post! Do you have an code sample?

best wishes from germany

Thomas