diff --git a/model/query_test.go b/model/query_test.go index f3fcced..6ce4967 100644 --- a/model/query_test.go +++ b/model/query_test.go @@ -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"` CommentCount int64 `gorm:"column:comment_count" db:"comment_count" json:"comment_count" form:"comment_count"` User *user + Ships *[]TermRelationships PostMeta *[]models.PostMeta - TermTaxonomy *[]models.TermTaxonomy + TermTaxonomy *[]TermTaxonomy } type TermRelationships struct { ObjectID uint64 `db:"object_id"` TermTaxonomyId uint64 `db:"term_taxonomy_id"` TermOrder int64 `db:"term_order"` + TermTaxonomy *[]TermTaxonomy } type user struct { diff --git a/model/relation.go b/model/relation.go index c00afff..70a8dd6 100644 --- a/model/relation.go +++ b/model/relation.go @@ -44,19 +44,12 @@ func parseBeforeJoin(qq *QueryCondition, ship Relationship) { var fromTable, foreignKey, local string if ship.Middle != nil { parseBeforeJoin(qq, *ship.Middle) - fromTable = ship.Middle.Table - foreignKey = ship.ForeignKey local = ship.Local } else { 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, " ") from := strings.Split(fromTable, " ") on := "" diff --git a/model/relation_test.go b/model/relation_test.go index 8d11bc7..93d7841 100644 --- a/model/relation_test.go +++ b/model/relation_test.go @@ -5,6 +5,63 @@ import ( "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 { 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 { return m.Id -}, func(p *models.TermTaxonomy) uint64 { +}, func(p *TermTaxonomy) uint64 { return p.TermTaxonomyId -}, func(m *post, i *[]models.TermTaxonomy) { +}, func(m *post, i *[]TermTaxonomy) { m.TermTaxonomy = i }, Relationship{ RelationType: HasOne, @@ -112,8 +169,13 @@ func TestGets2(t *testing.T) { ), PostAuthor2()), Fields("posts.*"), From("wp_posts posts"), - WithFn(true, false, nil, Meta2()), - WithFn(true, false, nil, term), + WithFn(true, true, nil, Meta2()), + 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 @@ -134,7 +196,12 @@ func TestGets2(t *testing.T) { Fields("posts.*"), From("wp_posts posts"), 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