db middle table

This commit is contained in:
xing 2023-05-24 21:33:30 +08:00
parent d5a546c01a
commit 95e3298b7e
3 changed files with 77 additions and 15 deletions

View File

@ -38,14 +38,16 @@ type post struct {
PostMimeType string `gorm:"column:post_mime_type" db:"post_mime_type" json:"post_mime_type" form:"post_mime_type"` PostMimeType string `gorm:"column:post_mime_type" db:"post_mime_type" json:"post_mime_type" form:"post_mime_type"`
CommentCount int64 `gorm:"column:comment_count" db:"comment_count" json:"comment_count" form:"comment_count"` CommentCount int64 `gorm:"column:comment_count" db:"comment_count" json:"comment_count" form:"comment_count"`
User *user User *user
Ships *[]TermRelationships
PostMeta *[]models.PostMeta PostMeta *[]models.PostMeta
TermTaxonomy *[]models.TermTaxonomy TermTaxonomy *[]TermTaxonomy
} }
type TermRelationships struct { type TermRelationships struct {
ObjectID uint64 `db:"object_id"` ObjectID uint64 `db:"object_id"`
TermTaxonomyId uint64 `db:"term_taxonomy_id"` TermTaxonomyId uint64 `db:"term_taxonomy_id"`
TermOrder int64 `db:"term_order"` TermOrder int64 `db:"term_order"`
TermTaxonomy *[]TermTaxonomy
} }
type user struct { type user struct {

View File

@ -44,19 +44,12 @@ func parseBeforeJoin(qq *QueryCondition, ship Relationship) {
var fromTable, foreignKey, local string var fromTable, foreignKey, local string
if ship.Middle != nil { if ship.Middle != nil {
parseBeforeJoin(qq, *ship.Middle) parseBeforeJoin(qq, *ship.Middle)
fromTable = ship.Middle.Table
foreignKey = ship.ForeignKey
local = ship.Local local = ship.Local
} else { } else {
fromTable = qq.From fromTable = qq.From
if ship.RelationType == HasMany {
foreignKey = ship.Local
local = ship.ForeignKey
} else {
foreignKey = ship.ForeignKey
local = ship.Local
}
} }
foreignKey = ship.ForeignKey
local = ship.Local
tables := strings.Split(ship.Table, " ") tables := strings.Split(ship.Table, " ")
from := strings.Split(fromTable, " ") from := strings.Split(fromTable, " ")
on := "" on := ""

View File

@ -5,6 +5,63 @@ import (
"testing" "testing"
) )
type TermTaxonomy struct {
TermTaxonomyId uint64 `gorm:"column:term_taxonomy_id" db:"term_taxonomy_id" json:"term_taxonomy_id" form:"term_taxonomy_id"`
TermId uint64 `gorm:"column:term_id" db:"term_id" json:"term_id" form:"term_id"`
Taxonomy string `gorm:"column:taxonomy" db:"taxonomy" json:"taxonomy" form:"taxonomy"`
Description string `gorm:"column:description" db:"description" json:"description" form:"description"`
Parent uint64 `gorm:"column:parent" db:"parent" json:"parent" form:"parent"`
Count int64 `gorm:"column:count" db:"count" json:"count" form:"count"`
Term *models.Terms
}
var termMyHasOneTerm = RelationHasOne(func(m *TermTaxonomy) uint64 {
return m.TermTaxonomyId
}, func(p *models.Terms) uint64 {
return p.TermId
}, func(m *TermTaxonomy, p *models.Terms) {
m.Term = p
}, Relationship{
RelationType: HasOne,
Table: "wp_terms",
ForeignKey: "term_id",
Local: "term_id",
})
var postHasManyShip = RelationHasMany(func(m *post) uint64 {
return m.Id
}, func(p *TermRelationships) uint64 {
return p.ObjectID
}, func(m *post, i *[]TermRelationships) {
m.Ships = i
}, Relationship{
RelationType: HasMany,
Table: "wp_term_relationships",
ForeignKey: "object_id",
Local: "ID",
})
var shipHasManyTermMy = RelationHasMany(func(m *TermRelationships) uint64 {
return m.TermTaxonomyId
}, func(p *TermTaxonomy) uint64 {
return p.TermTaxonomyId
}, func(m *TermRelationships, i *[]TermTaxonomy) {
m.TermTaxonomy = i
}, Relationship{
RelationType: HasMany,
Table: "wp_term_taxonomy",
ForeignKey: "term_taxonomy_id",
Local: "term_taxonomy_id",
})
func (w TermTaxonomy) PrimaryKey() string {
return "term_taxonomy_id"
}
func (w TermTaxonomy) Table() string {
return "wp_term_taxonomy"
}
func postAuthorId(p *post) uint64 { func postAuthorId(p *post) uint64 {
return p.PostAuthor return p.PostAuthor
} }
@ -62,9 +119,9 @@ func PostMetas() (func(any) []any, func(any, any), any, any, Relationship) {
var term = RelationHasMany(func(m *post) uint64 { var term = RelationHasMany(func(m *post) uint64 {
return m.Id return m.Id
}, func(p *models.TermTaxonomy) uint64 { }, func(p *TermTaxonomy) uint64 {
return p.TermTaxonomyId return p.TermTaxonomyId
}, func(m *post, i *[]models.TermTaxonomy) { }, func(m *post, i *[]TermTaxonomy) {
m.TermTaxonomy = i m.TermTaxonomy = i
}, Relationship{ }, Relationship{
RelationType: HasOne, RelationType: HasOne,
@ -112,8 +169,13 @@ func TestGets2(t *testing.T) {
), PostAuthor2()), ), PostAuthor2()),
Fields("posts.*"), Fields("posts.*"),
From("wp_posts posts"), From("wp_posts posts"),
WithFn(true, false, nil, Meta2()), WithFn(true, true, nil, Meta2()),
WithFn(true, false, nil, term), WithFn(true, false, Conditions(
WithFn(true, false, Conditions(
WithFn(true, false, nil, termMyHasOneTerm),
), shipHasManyTermMy),
), postHasManyShip),
//WithFn(true, false, nil, term),
) )
got, err := Gets[post](ctx, q) got, err := Gets[post](ctx, q)
_ = got _ = got
@ -134,7 +196,12 @@ func TestGets2(t *testing.T) {
Fields("posts.*"), Fields("posts.*"),
From("wp_posts posts"), From("wp_posts posts"),
WithFn(true, false, nil, Meta2()), WithFn(true, false, nil, Meta2()),
WithFn(true, false, nil, term), WithFn(true, false, Conditions(
WithFn(true, false, Conditions(
WithFn(true, false, nil, termMyHasOneTerm),
), shipHasManyTermMy),
), postHasManyShip),
//WithFn(true, false, nil, term),
) )
got, err := Finds[post](ctx, q) got, err := Finds[post](ctx, q)
_ = got _ = got