I've created a Magento 2 CRUD/Abstract model that updates the following MySQL table.

CREATE TABLE `pulsestorm_commercebug_log` (
  `pulsestorm_commercebug_log_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity ID',
  `json_log` mediumtext NOT NULL COMMENT 'Demo Title',
  `creation_time` timestamp NULL DEFAULT NULL COMMENT 'Creation Time',
  `update_time` timestamp NULL DEFAULT NULL COMMENT 'Modification Time',
  `is_active` smallint(6) NOT NULL DEFAULT '1' COMMENT 'Is Active',
  PRIMARY KEY (`pulsestorm_commercebug_log_id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COMMENT='pulsestorm_commercebug_log';

I can successfully use the model's save method to add data to this table. However, the creation_time and update_time columns are not updated. Their initial values are NULL.

Do I need to do something special with my models to get these creation and update times columns working? Does anyone that's run into similar problems know of a solution?

If not -- does anyone know where in Magento 2's code base the ORM normally populate these values? (so I can debug what's wrong with my Models there)

  • 1
    Why are you do not use CURRENT_TIMESTAMP as default value and on update?
    – KAndy
    Jan 23 '16 at 20:08
  • @KAndy SQL create came from copying something in core (can't remember what). Are you saying that the create/update functionality relies on CURRENT_TIMESTAMP being set for the default value and update value? i.e. its not something manager on the ORM level?
    – Alan Storm
    Jan 23 '16 at 20:24

It turns out (thanks KAndy!) that the creation and update timestamps are not handled on the ORM level. If you want this functionality, you need to make sure your database tables are created with the correct timestamp attributes. i.e., in your install schema, something like.

        ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],
        'Creation Time'
        ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],
        'Modification Time'

There is another example of how to achieve this inside the wishlist module. Look in the beforeSave() method on Model/Wishlist.php. Here is the code snippet

 * Set date of last update for wishlist
 * @return $this
public function beforeSave()
    return $this;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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