`
coolsooner
  • 浏览: 1314805 次
文章分类
社区版块
存档分类
最新评论

magento----时间处理函数----beforesave----不错的函数

 
阅读更多

很多存入数据库的数据是程序自己生成的,不需要用户填写的,譬如时间,具体的实现是通过事件实现的,譬如下面的是一个事件函数,在保存前执行的函数_beforeSave();

改函数的作用,自己生成时间参数---creation_time,UpdateTime,进而存入对象属性值。

使用的magento类:mage_core_model_locale

lib-varien-object函数:varien_date。

/**
* Process page data before saving
*
* @param Mage_Core_Model_Abstract $object
*/
protected function _beforeSave(Mage_Core_Model_Abstract $object)
{

if (! $object->getId() && $object->getCreationTime() == "") {

//1
$object->setCreationTime(Mage::getSingleton('core/date')->gmtDate());
}
//2
$format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);


if ($date = $object->getData('creation_time')) {

//3
$object->setData('creation_time', Mage::app()->getLocale()->date($date, $format, null, false)
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT)
);
}


$object->setUpdateTime(Mage::getSingleton('core/date')->gmtDate());

return $this;
}

//1

使用locale.php里面的gmtDate();

//2

Mage:app()得到mage_core_model_app,---->getlocale()得到mage_core_model_locale

执行这个class下的getDateFormat(),

//3

执行mage_core_model_locale下的date函数,参数为//1 和//2的值,

这是locale内部的一些列的函数的使用,locale是一系列的本地功能的集合。辅助性!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics