import scaffoldings
This commit is contained in:
12
daos/base.go
12
daos/base.go
@@ -128,7 +128,7 @@ func (dao *Dao) Delete(m models.Model) error {
|
||||
|
||||
// Save upserts (update or create if primary key is not set) the provided model.
|
||||
func (dao *Dao) Save(m models.Model) error {
|
||||
if m.HasId() {
|
||||
if !m.IsNew() {
|
||||
return dao.update(m)
|
||||
}
|
||||
|
||||
@@ -140,6 +140,10 @@ func (dao *Dao) update(m models.Model) error {
|
||||
return errors.New("ID is not set")
|
||||
}
|
||||
|
||||
if m.GetCreated().IsZero() {
|
||||
m.RefreshCreated()
|
||||
}
|
||||
|
||||
m.RefreshUpdated()
|
||||
|
||||
if dao.BeforeUpdateFunc != nil {
|
||||
@@ -195,6 +199,9 @@ func (dao *Dao) create(m models.Model) error {
|
||||
|
||||
if v, ok := any(m).(models.ColumnValueMapper); ok {
|
||||
dataMap := v.ColumnValueMap()
|
||||
if _, ok := dataMap["id"]; !ok {
|
||||
dataMap["id"] = m.GetId()
|
||||
}
|
||||
|
||||
_, err := dao.db.Insert(m.TableName(), dataMap).Execute()
|
||||
if err != nil {
|
||||
@@ -206,6 +213,9 @@ func (dao *Dao) create(m models.Model) error {
|
||||
}
|
||||
}
|
||||
|
||||
// clears the internal isNewFlag
|
||||
m.UnmarkAsNew()
|
||||
|
||||
if dao.AfterCreateFunc != nil {
|
||||
dao.AfterCreateFunc(dao, m)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user