php - 如何使用 codeigniter 3 中的 dbforge 类在时间戳字段中设置更新属性

标签 php mysql sql codeigniter codeigniter-3

我想设置更新属性以获取我的应用程序的更新时间。这是我的模型代码:

public function createDatabase() {
    $testdatabase = "testdatabase";
    $this->dbforge->create_database($testdatabase);
    $this->db->db_select($testdatabase);
    $fields = array(
        'id' => array(
            'type' => 'INT',
            'constraint' => 1,
            'unsigned' => TRUE,
        ),
        'name' => array(
            'type' => 'VARCHAR',
            'constraint' => '100',
        ),
        'establishment_date' => array(
            'type' => 'VARCHAR',
            'constraint' => '50',
            'null' => TRUE,
        ),
        'package' => array(
            'type' => 'VARCHAR',
            'constraint' => '15',
        ),
        'db_name' => array(
            'type' => 'VARCHAR',
            'constraint' => '25',
        ),
        'update_date' => array(
            'type' => 'datetime',
            'on update' => 'NOW()',
        ),
        'upgrade' => array(
            'type' => 'tinyint',
            'constraint' => '1',
            'default' => '0',
        ),
        'status' => array(
            'type' => 'tinyint',
            'constraint' => '1',
            'default' => '4',
        ),
    );
    $this->dbforge->add_field($fields);
    $this->dbforge->add_field("subscription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP");
    $this->dbforge->add_field("subscription_expire TIMESTAMP DEFAULT '0000-00-00 00:00:00'");
    $this->dbforge->add_key('id', TRUE);
    $this->dbforge->create_table('tms_profile');
}

表已成功创建,但更新属性在名为 update_date 的字段上不存在。我想在字段上设置更新属性。

最佳答案

您所要做的就是更改 update_date 列的 type

....
'update_date' => array(
       'type' => 'TIMESTAMP'
 ),
.....

现在它将在更新时使用 NOW()

关于php - 如何使用 codeigniter 3 中的 dbforge 类在时间戳字段中设置更新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40399845/

相关文章:

php - 如何将表单数据作为外键插入表中?

mysql - 带有追加模式的 tee 命令从 mysql 命令行给出错误

mysql - 如何使用mysql计算两个日期之间的星期日数

php - 将 Session 变量从 Ajax 发送到 PHP

php - 包装文档/文字数组的示例

php - MySQL选择表中特定id的两个时间戳列之间的平均时间差

mysql - 什么时候同时使用外键作为主键?

sql - PostgreSQL : Weighted ordering, 按列A * 0.7 + 列B * 0.3 排序?

php - 验证中 AJAX 的安全风险?

php - 无法在数据透视表中创建多个多对多关系