From fe9ac0d126aa1f9e721a123095c7c1453e4a71bf Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 29 May 2023 19:40:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/query_test.go | 1 + model/relation_test.go | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/model/query_test.go b/model/query_test.go index 0b14e93..a691c34 100644 --- a/model/query_test.go +++ b/model/query_test.go @@ -42,6 +42,7 @@ type post struct { PostMeta *[]models.PostMeta TermTaxonomy *[]TermTaxonomy Terms *[]models.Terms + CommentMetas *[]CommentMeta } type TermRelationships struct { diff --git a/model/relation_test.go b/model/relation_test.go index d0b9023..dd51587 100644 --- a/model/relation_test.go +++ b/model/relation_test.go @@ -16,6 +16,13 @@ type TermTaxonomy struct { Term *models.Terms } +type CommentMeta struct { + MetaId uint64 `db:"meta_id"` + CommentId uint64 `db:"comment_id"` + MetaKey string `db:"meta_key"` + MetaValue string `db:"meta_value"` +} + var termMyHasOneTerm = RelationHasOne(func(m *TermTaxonomy) uint64 { return m.TermTaxonomyId }, func(p *models.Terms) uint64 { @@ -155,6 +162,34 @@ var postHaveManyTerms = RelationHasMany(func(m *post) uint64 { }, }) +var postHaveManyCommentMetas = func() RelationFn { + type metas struct { + CommentPostID uint64 `db:"comment_post_ID"` + CommentMeta + } + return RelationHasMany(func(m *post) uint64 { + return m.Id + }, func(p *metas) uint64 { + return p.CommentPostID + }, func(m *post, i *[]metas) { + v := slice.Map(*i, func(t metas) CommentMeta { + return t.CommentMeta + }) + m.CommentMetas = &v + }, Relationship{ + RelationType: HasOne, + Table: "wp_commentmeta", + ForeignKey: "comment_id", + Local: "comment_ID", + Middle: &Relationship{ + RelationType: HasMany, + Table: "wp_comments comments", + ForeignKey: "comment_post_ID", + Local: "ID", + }, + }) +}() + func Meta2() RelationFn { return RelationHasMany(postId, metasPostId, func(m *post, i *[]models.PostMeta) { m.PostMeta = i @@ -207,7 +242,7 @@ func TestGets2(t *testing.T) { { q := Conditions( Where(SqlBuilder{{"posts.id", "in", ""}}), - In([]any{190, 3022, 291}), + In([]any{190, 3022, 291, 2858}), WithCtx(&ctx), WithFn(true, false, Conditions( Fields("ID,user_login,user_pass"), @@ -221,6 +256,7 @@ func TestGets2(t *testing.T) { ), shipHasManyTermMy), ), postHasManyShip),*/ WithFn(true, false, nil, postHaveManyTerms), + WithFn(true, false, nil, postHaveManyCommentMetas), ) got, err := Finds[post](ctx, q) _ = got