Skip to content

DIY method call model Method? #724

@hoslo

Description

@hoslo

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 modify_time=@wrong.Now() WHERE id = @wrong.Id
	UpdateByID(user *gen.T) error
}

Expected answer

now is this

// UPDATE @@table SET modify_time=@wrong.Now() 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 modify_time=@wrong.Now() 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
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions