From 19c0952005a9f47b9209308e75efc14d95047666 Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 9 Jun 2023 22:28:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E8=81=94=E5=B7=B2=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=9A=84=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/relation.go | 18 ++++++++++++++++++ model/relation_test.go | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/model/relation.go b/model/relation.go index 32bfdc8..655a528 100644 --- a/model/relation.go +++ b/model/relation.go @@ -273,3 +273,21 @@ func AddRelationFn(getVal, join bool, q *QueryCondition, r RelationFn) func() (b return getVal, join, q, r } } + +func withOther(db dbQuery, ctx context.Context, r any, q *QueryCondition) error { + _, after := Relation(true, db, ctx, r, q) + for _, fn := range after { + err := fn() + if err != nil { + return err + } + } + return nil +} +func DBWithOther(db dbQuery, ctx context.Context, r any, q *QueryCondition) error { + return withOther(db, ctx, r, q) +} + +func WithOther(ctx context.Context, r any, q *QueryCondition) error { + return withOther(globalBb, ctx, r, q) +} diff --git a/model/relation_test.go b/model/relation_test.go index 907d71e..d102ce7 100644 --- a/model/relation_test.go +++ b/model/relation_test.go @@ -266,3 +266,21 @@ func TestGets2(t *testing.T) { } }) } + +func TestWithOther(t *testing.T) { + t.Run("hasMany", func(t *testing.T) { + r, err := Finds[post](ctx, Conditions( + Where(SqlBuilder{{"id", "in", ""}}), + In([]any{190, 3022, 291, 2858}), + )) + if err != nil { + t.Fatalf("fatal:%v", err) + } + if err = WithOther(ctx, &r, Conditions( + WithFn(true, false, nil, postHaveManyTerms), + WithFn(true, false, nil, postHaveManyCommentMetas), + )); err != nil { + t.Errorf("WithOther() error = %v", err) + } + }) +}