简体   繁体   English

如何使用 CodeIgniter DB Forge 在 MySQL 中创建 DATETIME?

[英]How to create DATETIME filed in MySQL with CodeIgniter DB Forge?

I'm learning CodeIgniter and now I wan to create MySQL table with DATETIME field.我正在学习 CodeIgniter,现在我想用 DATETIME 字段创建 MySQL 表。

I have this code in Install_Model.php我在 Install_Model.php 中有这个代码

    $Fields = array(
      'ID' => array(
      'Type' => 'INT',
      'Constraint' => 10,
      'Unsigned' => TRUE,
      'Auto_Increment' => TRUE
    ),
      'Username' => array(
      'Type' => 'VARCHAR',
      'Constraint' => '255'
    ),
      'Password' => array(
      'Type' => 'VARCHAR',
      'Constraint' => '255'
    ),
      'AddDate' => array(
      'Type' => 'DATETIME'
    )
  );

  $this->dbforge->add_key('ID', TRUE);
  $this->dbforge->add_field($Fields);
  $this->dbforge->create_table('Admins', TRUE);
  echo 'Table Admins created successfully!<br/>';

When I execute this code I get this error:当我执行此代码时,出现此错误:

Warning: #1265 Data truncated for column 'AddDate' at row 1警告:#1265 第 1 行“AddDate”列的数据被截断

And in filed AddDate content is 0000-00-00 00:00:00而在提交的AddDate内容是0000-00-00 00:00:00

How I can fix problem, and make AddDate content current timestamp?我如何解决问题,并使AddDate内容成为当前时间戳?

I tried to set DEFAULT CURRENT_TIMESTAMP but then CodeIgniter returns error in SQL query :(我尝试设置 DEFAULT CURRENT_TIMESTAMP 但随后 CodeIgniter 在 SQL 查询中返回错误:(

尝试

'AddDate' => array( 'type' => 'datetime', 'default' => '0000-00-00 00:00:00', )

'AddDate' => array(
        'type'       => 'TIMESTAMP',
    )

Add these in your model, it will automatically add current datetime on creation, updation adn deletion etc.在您的模型中添加这些,它会在创建、更新和删除等时自动添加当前日期时间。

protected $createdField  = 'AddDate';
protected $updatedField  = 'updated_at';
protected $deletedField  = 'deleted_at';

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

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