简体   繁体   中英

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.

I have this code in 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

And in filed AddDate content is 0000-00-00 00:00:00

How I can fix problem, and make AddDate content current timestamp?

I tried to set DEFAULT CURRENT_TIMESTAMP but then CodeIgniter returns error in SQL query :(

尝试

'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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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