简体   繁体   English

如何使用CodeIgniter DB Forge生成时间戳和日期时间的createdAt和updatedAt?

[英]How to generate createdAt and updatedAt timestamps/datetime using CodeIgniter DB Forge?

I would like to have 2 timestamp/datetime fields, createdAt which only contains the time when the record was created but doesn't update constantly if there are updates on the record and updatedAt which generates during the time when the record was created and everytime the record gets updated. 我想有2个timestamp / datetime字段, createdAt仅包含创建记录的时间,但是如果记录上有更新,则不会持续更新,而updateAt在创建记录时以及每次生成时都会更新记录被更新。 It's also said that a record should only contain one timestamp field at least for MySQL? 也有人说,一条记录至少应只包含一个时间戳字段(至少对于MySQL)?

Here is my migration code: 这是我的迁移代码:

$this->dbforge->add_field(array(
    'id' => array(
        'type' => 'INT',
        'constraint' => 5,
        'unsigned' => TRUE,
        'auto_increment' => TRUE
    ),
    'name' => array(
        'type' => 'VARCHAR',
        'constraint' => '50',
    ),
    'createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP'
));

How is this done using CodeIgniter DB Forge? 如何使用CodeIgniter DB Forge完成此操作? Any answers are well appreciated. 任何答案都很好。 Thanks 谢谢

Okay I got it, here is my solution: 好的,知道了,这是我的解决方案:

$this->dbforge->add_field(array(
    'id' => array(
        'type' => 'INT',
        'constraint' => 5,
        'unsigned' => TRUE,
        'auto_increment' => TRUE
    ),
    'name' => array(
        'type' => 'VARCHAR',
        'constraint' => '50',
    ),
    'createdAt timestamp default now()',
    'updatedAt timestamp default now() on update now()'
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM