### 創建時間Created
Created可以讓您在數據插入到數據庫時自動將對應的字段設置為當前時間,需要在xorm標記中使用created標記,如下所示進行標記,對應的字段可以為time.Time或者自定義的time.Time或者int,int64等int類型。
~~~
type User struct {
Id int64
Name string
CreatedAt time.Time `xorm:"created"`
}
~~~
或
~~~
type JsonTime time.Time
func (j JsonTime) MarshalJSON() ([]byte, error) {
return []byte(`"`+time.Time(j).Format("2006-01-02 15:04:05")+`"`), nil
}
type User struct {
Id int64
Name string
CreatedAt JsonTime `xorm:"created"`
}
~~~
或
~~~
type User struct {
Id int64
Name string
CreatedAt int64 `xorm:"created"`
}
~~~
在Insert()或InsertOne()方法被調用時,created標記的字段將會被自動更新為當前時間或者當前時間的秒數(對應為time.Unix()),如下所示:
~~~
var user User
engine.Insert(&user)
// INSERT user (created...) VALUES (?...)
~~~