自定义updated_at字段没有';工作目录ventory_stock_item current_timesta


custom updated_at field doesn't work cataloginventory_stock_item current_timestamp

我在cataloginventory_stock_item中添加了一个额外的字段,以便在库存变化时更新到当前时间:

 Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
          $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
          $installer->startSetup();
          $installer->getConnection()->addColumn(
          $installer->getTable('cataloginventory_stock_item'),
            'updated_time',
            'TIMESTAMP ON UPDATE NOW() NOT NULL DEFAULT NOW()'
          ); 
        $installer->endSetup();

但是,当数量在magento中发生变化时,字段不会更新到当前时间戳(但数量确实发生了变化)。

我还尝试使用CURRENT_TIMESTAMP而不是NOW() 创建一个字段

我相信只有当您不为该列提供值时,ON UPDATE行为才会由MySQL触发,由于Magento模型使用Varien_Object的方式,从数据库加载的值将在您保存模型时发布。如果你想更新它,最好的办法可能是为cataloginventory_stock_item_before_save注册一个观察者,并使用setData将updated_time设置为当前时间,然后再将其保存到数据库中。

您可以在保存模型之前通过侦听cataloginventory_stock_item_save_before事件来更新模型的updated_time数据值。

您可以执行以下操作:

$item->setUpdatedAt(0);

与手动设置时间戳没有太大区别,但它由数据库来设置

如果是自定义模型,您也可以在模型中执行以下操作:

protected function _beforeSave(Mage_Core_Model_Abstract $model)
{
    $model->setUpdatedAt(0);
}