gen icon indicating copy to clipboard operation
gen copied to clipboard

DIY method call model Method?

Open hoslo opened this issue 3 years ago • 0 comments

Your Question

can i call the model's method in the diy method template?

The document you expected this should be explained

type User struct {
	Id         uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"`
	CreateTime int64  `gorm:"column:create_time;type:bigint(20) unsigned;NOT NULL" json:"create_time"`
}

func (w User) Now() int64 {
	return time.Now().Unix()
}
type Method interface {
	// UPDATE @@table SET [email protected]() WHERE id = @wrong.Id
	UpdateByID(user *gen.T) error
}

Expected answer

now is this

// UPDATE @@table SET [email protected]() WHERE id = @wrong.Id
func (u userDo) UpdateByID(wrong *model.User) (err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, wrong.Now)
	params = append(params, wrong.Id)
	generateSQL.WriteString("UPDATE users SET modify_time=?() WHERE id = ? ")

	var executeSQL *gorm.DB

	executeSQL = u.UnderlyingDB().Exec(generateSQL.String(), params...)
	err = executeSQL.Error
	return
}

want this

// UPDATE @@table SET [email protected]() WHERE id = @wrong.Id
func (u userDo) UpdateByID(wrong *model.User) (err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, wrong.Now())
	params = append(params, wrong.Id)
	generateSQL.WriteString("UPDATE users SET modify_time=? WHERE id = ? ")

	var executeSQL *gorm.DB

	executeSQL = u.UnderlyingDB().Exec(generateSQL.String(), params...)
	err = executeSQL.Error
	return
}

hoslo avatar Dec 18 '22 03:12 hoslo