如何为MySQL Datetime列设置默认值?
在SQL Server中它是getdate().什么是MySQL的等价物?如果这是一个因素,我正在使用MySQL 5.x.
我正在使用Doctrine 2和Codeigniter 2,我想Doctrine自动生成表格中给定字段中插入的当前日期.
课程文件:
<?php
namespace models;
/**
* @Entity
* @Table(name="workers")
*/
class Workers {
/**
* @Id
* @Column(type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Column(type="string", length=255, unique=true, nullable=false)
*/
protected $email;
/**
* @var datetime $created_on
*
* @gedmo:Timestampable(on="create")
* @Column(type="datetime")
*/
protected $created_on;
/** @PrePersist */
function onPrePersist()
{
$this->created_on = date('Y-m-d H:i:s');
}
/* Setters & Getters */
public function setEmail($email){ $this->email = $email; }
public function getEmail(){ return $this->email; }
}
Run Code Online (Sandbox Code Playgroud)
插入方法: …