bee icon indicating copy to clipboard operation
bee copied to clipboard

关于beego框架的orm问题

Open kathe-jing opened this issue 11 years ago • 0 comments

@astaxie 关于beego框架中数据表的一对多关系有个问题想请教一下,具体如下:

我现在有两张表,policy和rule,policy和rule时间是一对多的关系,model的定义如下:

type Policy struct { Id int orm:"pk;auto" Rules []*Rule orm:"reverse(many)" ServiceInstanceId string orm:"size(64)" Name string orm:"size(64)" MinInstance int MaxInstance int StatisticWin int BreachDur int CooldownOut int CooldownIn int LastScaleoutTime time.Time LastScaleinTime time.Time }

type Rule struct { Id int orm:"pk;auto" Policy *Policy orm:"rel(fk)" MetricType string orm:"size(16);" ScaleOutThreshold int ScaleOutUnit int ScaleInThreshold int ScaleInUnit int }

现在http请求的body体的格式如下:

​{ "Rules": [ { "MtricType": "CPU", "ScaleOutThreshold": 80, "ScaleOutUnit": 1, "ScaleInThreshold": 30, "ScaleInUnit": 5 }, { "MtricType": "Memory", "ScaleOutThreshold": 80, "ScaleOutUnit": 1, "ScaleInThreshold": 30, "ScaleInUnit": 1 } ], "ServiceInstanceId": "fb41bf4a-89c6-4a73-9f3b-1f8957215a52", "Name": "policy_1", "MinInstance": 1, "MaxInstance": 5, "StatisticWin": 120, "BreachDur": 300, "CooldownOut": 300, "CooldownIn": 300 }

现在我要解析body体,​err := json.Unmarshal(this.Ctx.Input.RequestBody, &policy),最后执行policy的save,但是rule却没有更新,我看到policy和rule之间定义一对多的关系时,使用的是结构体指针的数组和结构体的指针,与body的格式不一样,请问如果我想解析出的policy带有rule的指针数组,并且save policy的时候不仅更新policy表,还更新相应的rule表,请问这样可以实现吗?​

kathe-jing avatar Jul 01 '14 12:07 kathe-jing