关联已查询的结果

This commit is contained in:
xing 2023-06-09 22:28:23 +08:00
parent 3493eee919
commit 19c0952005
2 changed files with 36 additions and 0 deletions

View File

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

View File

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