Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a15234

Browse files
committed
没有pk,incr更新会有问题
1 parent 6d7d3c2 commit 6a15234

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/logic/topic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ func (self TopicComment) UpdateComment(cid, objid, uid int, cmttime time.Time) {
455455
}
456456

457457
// 更新回复数(TODO:暂时每次都更新表)
458-
_, err = MasterDB.Where("tid", objid).Incr("reply", 1).Update(new(model.TopicEx))
458+
_, err = MasterDB.Id(objid).Incr("reply", 1).Update(new(model.TopicUpEx))
459459
if err != nil {
460460
logger.Errorln("更新主题回复数失败:", err)
461461
session.Rollback()
@@ -496,7 +496,7 @@ type TopicLike struct{}
496496
// objid:被喜欢对象id;num: 喜欢数(负数表示取消喜欢)
497497
func (self TopicLike) UpdateLike(objid, num int) {
498498
// 更新喜欢数(TODO:暂时每次都更新表)
499-
_, err := MasterDB.Where("tid=?", objid).Incr("like", num).Update(new(model.TopicEx))
499+
_, err := MasterDB.Where("tid=?", objid).Incr("like", num).Update(new(model.TopicUpEx))
500500
if err != nil {
501501
logger.Errorln("更新主题喜欢数失败:", err)
502502
}

src/logic/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (this *view) flush() {
5050
session := MasterDB.Id(this.objid)
5151
switch this.objtype {
5252
case model.TypeTopic:
53-
session.Where("tid", this.objid).Incr("view", this.num).Update(new(model.TopicEx))
53+
session.Incr("view", this.num).Update(new(model.TopicUpEx))
5454
case model.TypeArticle:
5555
session.Incr("viewnum", this.num).Update(new(model.Article))
5656
case model.TypeResource:

src/model/topic.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ func (*TopicEx) TableName() string {
6060
return "topics_ex"
6161
}
6262

63+
// 社区主题扩展(计数)信息,用于 incr 更新
64+
type TopicUpEx struct {
65+
Tid int `json:"-" xorm:"pk"`
66+
View int `json:"view"`
67+
Reply int `json:"reply"`
68+
Like int `json:"like"`
69+
Mtime time.Time `json:"mtime" xorm:"<-"`
70+
}
71+
72+
func (*TopicUpEx) TableName() string {
73+
return "topics_ex"
74+
}
75+
6376
type TopicInfo struct {
6477
Topic `xorm:"extends"`
6578
TopicEx `xorm:"extends"`

0 commit comments

Comments
 (0)