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) + } + }) +}