admin
admin copied to clipboard
Not working association for Gorm models
Hello! I have two models
type Event struct {
gorm.Model
Title string `json:"title"`
About string `json:"about"`
StartDate *time.Time `json:"start_date"`
EndDate *time.Time `json:"end_date"`
Type string `json:"type"`
Photos []Photo `json:"photos""`
Comments []Comment `json:"comments"`
Checkins []User `json:"checkins"`
UserID uint `json:"user_id"`
location.Location `location:"name:Event place`
}
and
type Photo struct {
gorm.Model
EventID uint `json:"event_id"`
UserID uint `json:"user_id"`
Image oss.OSS `sql:"size:4294967295;" media_library:"url:/backend/{{class}}/{{primary_key}}/{{column}}.{{extension}};path:./private" json:"image"`
}
Also I implemented this func
func (event Event) MainImageURL(styles ...string) string {
if len(event.Photos) > 0 {
return event.Photos[0].Image.URL()
}
return ""
}
But there are no photos in event model, it seems association not working. What I do wrong?
and valuer not called
ev.Meta(&admin.Meta{Name: "MainImage", FieldName: "MainImage", Label: "MainImage", Valuer: func(record interface{}, context *qor.Context) interface{} {
if p, ok := record.(*models.Event); ok {
result := bytes.NewBufferString("")
tmpl, _ := template.New("").Parse("<img src='{{.image}}'></img>")
tmpl.Execute(result, map[string]string{"image": p.MainImage()})
return template.HTML(result.String())
}
return ""
}})