很多存入数据库的数据是程序自己生成的,不需要用户填写的,譬如时间,具体的实现是通过事件实现的,譬如下面的是一个事件函数,在保存前执行的函数_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是一系列的本地功能的集合。辅助性!
本文介绍了一个Magento系统中用于在保存数据库记录前自动生成创建时间和更新时间的事件函数。该函数利用Magento的核心类和日期格式化功能确保了时间戳的一致性和准确性。
958

被折叠的 条评论
为什么被折叠?



