0

Synopsis

I have a date field in my admin form but when I save this date & time, I then have an observer that runs on my model _beforeSave which corrects the format for MySQL. Although in the database and in the fields after reload, the time is missing.

Observer

<?php

require_once Mage::getModuleDir('', 'Some_Banners') . '/Model/Slide.php';

class My_Banner_Model_slide extends Some_Banners_Model_Slide
{
    public function _beforeSave()
    {
        parent::_beforeSave();

        if (preg_match('/^\d{2}([\/]+)\d{2}\1\d{4}\s+\d{1,2}\:+\d{1,2}$/', $this->getEndAt())) {
            $endDate = preg_replace('/^(\d{2}?)\/(\d{2}?)\/(\d{4}?)\s+(\d{1,2}?)\:+(\d{1,2}?)$/', '\\3-\\2-\\1 \\4:\\5:00', $this->getEndAt());
            $this->setData('end_at', $endDate);
        }

        return $this;
    }
}

Please note: when I execute the following debugging the data is correct as expected, something happens after this method before the save occurs.

if (preg_match('/^\d{2}([\/]+)\d{2}\1\d{4}\s+\d{1,2}\:+\d{1,2}$/', $this->getEndAt())) {
    Zend_Debug::dump( $this->getEndAt() );
    // Output: 13/05/2013 12:30

    $endDate = preg_replace('/^(\d{2}?)\/(\d{2}?)\/(\d{4}?)\s+(\d{1,2}?)\:+(\d{1,2}?)$/', '\\3-\\2-\\1 \\4:\\5:00', $this->getEndAt());

    Zend_Debug::dump( $endDate );
    // Output: 2015-05-13 12:30:00

    $this->setData('end_at', $endDate);

    Zend_Debug::dump( $this->getEndAt() );
    // Output: 2015-05-13 12:30:00
}
5
  • Zend_Db caches the database tables, did you make sure you went to "Cache Management" and flushed the "Magento Cache"? May 13, 2015 at 15:26
  • It's mysteriously worked, I did not clear the cache. Also, in my case the field was added and the date was being saved just not the time. Maybe it was cache.
    – Ash
    May 13, 2015 at 15:32
  • It could well be. Did you change the field from a DATE to a DATETIME maybe? May 13, 2015 at 15:39
  • Now you mention it, that would make sense. i had a date field, then changed to datetime didn't clear the cache so magento remembered the field as 'date'. Although, I never cleared the cache when it started to work - which is the confusing thing.
    – Ash
    May 13, 2015 at 16:06
  • The cache must have an expiry. I m glad your issue is resolved anyway, I will add my comment as an answer, if you wouldnt mind accepting it so we can close this. May 13, 2015 at 17:20

1 Answer 1

1

Zend_Db caches the database tables, make sure you go to "Cache Management" and flush the "Magento Cache"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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