attachment.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package models
  2. import (
  3. "time"
  4. "github.com/lifei6671/godoc/conf"
  5. "github.com/astaxie/beego/orm"
  6. )
  7. // Attachment struct .
  8. type Attachment struct {
  9. AttachmentId int `orm:"column(attachment_id);pk;auto;unique" json:"attachment_id"`
  10. DocumentId int `orm:"column(document_id);type(int)" json:"document_id"`
  11. FileName string `orm:"column(file_name);size(2000)" json:"file_name"`
  12. FileSize float64 `orm:"column(file_size);type(float)" json:"file_size"`
  13. FileExt string `orm:"column(file_ext);size(50)" json:"file_ext"`
  14. CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add"`
  15. CreateAt int `orm:"column(create_at);type(int)" json:"create_at"`
  16. }
  17. // TableName 获取对应数据库表名.
  18. func (m *Attachment) TableName() string {
  19. return "attachment"
  20. }
  21. // TableEngine 获取数据使用的引擎.
  22. func (m *Attachment) TableEngine() string {
  23. return "INNODB"
  24. }
  25. func (m *Attachment) TableNameWithPrefix() string {
  26. return conf.GetDatabasePrefix() + m.TableName()
  27. }
  28. func NewAttachment() *Attachment {
  29. return &Attachment{}
  30. }
  31. func (m *Attachment) Insert() error {
  32. o := orm.NewOrm()
  33. _,err := o.Insert(m)
  34. return err
  35. }
  36. func (m *Attachment) Find(id int) (*Attachment,error) {
  37. if id <= 0 {
  38. return m,ErrInvalidParameter
  39. }
  40. o := orm.NewOrm()
  41. err := o.QueryTable(m.TableNameWithPrefix()).Filter("attachment_id",id).One(m)
  42. return m,err
  43. }