Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

this is a module for eav module for go gorm , just support mysql now go gorm 的eav 模型, 目前只支持mysql

NotificationsYou must be signed in to change notification settings

jake-bcn/go-models

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

this is a module for eav module for go gorm , just support mysql nowgo gorm 的eav 模型, 目前只支持mysql

usage 用法

example 例子

BasicModelInterface 的實現

// define connectioin name// 定義鏈接名字vartestConnectionNamestring// define struct// 定義模型結構體typeUserTeststruct {EntityIduint64NamestringAgeuint32}// define table of db// 定義數據庫名字typeUserTeststruct {EntityIduint64NamestringAgeuint32CreatedAt time.TimeUpdatedAt time.Time}// 返回主表名字func (e*UserTest)GetTableName()string {return"user"}// 返回字段  , 會通過反射 reflect 設置 struct 字段/**  Name: struct 的字段名字 必須是public 的字段  IsEav: 是否為eav 字段   DbType: 跟struct 字段一樣類型   EavType: 會根據主表名字+ EavType 來確定 eav 的value 表   Autocreated:  只爲 time.Time 類型處理; 首次創建時會生成   Autoupdate: 只爲 time.Time 類型處理; 每次保存時會生成*/func (e*UserTest)GetTableFields()map[string]Field {fields:=map[string]Field{"entity_id":  {Name:"EntityId",IsEav:false,DbType:"uint64"},"name":       {Name:"Name",IsEav:true,DbType:"string",EavType:"varchar"},"age":        {Name:"Age",IsEav:false,DbType:"uint32"},"created_at": {Name:"CreatedAt",IsEav:false,DbType:"time.Time",Autocreate:true},"updated_at": {Name:"UpdatedAt",IsEav:false,DbType:"time.Time",Autocreate:true,Autoupdate:true},}returnfields}// 主表的 primary keyfunc (e*UserTest)GetPrimaryFieldName()string {return"entity_id"}

輔助轉換函數和 factory

// 將 Basictablemodelinterface 轉化為對應類型funcConvertModelToUserTest(tableModelBasictablemodelinterface)*UserTest {model:=tableModel.GetModel()ifm,ok:=model.(*UserTest);ok {returnm}return&UserTest{}}// 獲取 collectionfuncGetUserTestCollectionFactory(localestring,defaultLocalestring)CollectionInterface {callback:=func()Basictablemodelinterface {model:=&Basictablemodel{Model:&UserTest{},Connection:testConnectionName,Locale:locale,DefaultLocale:defaultLocale}returnmodel}returnCollectionFactory(callback)}// 獲取 BasictablemodelinterfacefuncGetUserTestFactory(localestring,defaultLocalestring)Basictablemodelinterface {callback:=func()Basictablemodelinterface {model:=&Basictablemodel{Model:&UserTest{},Connection:testConnectionName,Locale:locale,DefaultLocale:defaultLocale}returnmodel}returnModelFactory(callback)}

BasicModelLoadInterface 實現

func (e*UserTest)AfterLoad(tablemodelBasictablemodelinterface){}

BasicModelDeleteInterface 實現

func (e*UserTest)AfterDelete(tablemodelBasictablemodelinterface){}

BasicModelSaveInterface 實現

func (e*UserTest)AfterSave(tablemodelBasictablemodelinterface){}

BasicModelBeforeSaveInterface

func (e*UserTest)BeforeSave(tablemodelBasictablemodelinterface) {iftablemodel.GetData("name")=="error" {panic("cannot save name if error")}}

About

this is a module for eav module for go gorm , just support mysql now go gorm 的eav 模型, 目前只支持mysql

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp