| 606 | } |
| 607 | |
| 608 | func (association *Association) buildCondition() *DB { |
| 609 | var ( |
| 610 | queryConds = association.Relationship.ToQueryConditions(association.DB.Statement.Context, association.DB.Statement.ReflectValue) |
| 611 | modelValue = reflect.New(association.Relationship.FieldSchema.ModelType).Interface() |
| 612 | tx = association.DB.Model(modelValue) |
| 613 | ) |
| 614 | |
| 615 | if association.Relationship.JoinTable != nil { |
| 616 | if !tx.Statement.Unscoped && len(association.Relationship.JoinTable.QueryClauses) > 0 { |
| 617 | joinStmt := Statement{DB: tx, Context: tx.Statement.Context, Schema: association.Relationship.JoinTable, Table: association.Relationship.JoinTable.Table, Clauses: map[string]clause.Clause{}} |
| 618 | for _, queryClause := range association.Relationship.JoinTable.QueryClauses { |
| 619 | joinStmt.AddClause(queryClause) |
| 620 | } |
| 621 | joinStmt.Build("WHERE") |
| 622 | if len(joinStmt.SQL.String()) > 0 { |
| 623 | tx.Clauses(clause.Expr{SQL: strings.Replace(joinStmt.SQL.String(), "WHERE ", "", 1), Vars: joinStmt.Vars}) |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | tx = tx.Session(&Session{QueryFields: true}).Clauses(clause.From{Joins: []clause.Join{{ |
| 628 | Table: clause.Table{Name: association.Relationship.JoinTable.Table}, |
| 629 | ON: clause.Where{Exprs: queryConds}, |
| 630 | }}}) |
| 631 | } else { |
| 632 | tx.Clauses(clause.Where{Exprs: queryConds}) |
| 633 | } |
| 634 | |
| 635 | return tx |
| 636 | } |
| 637 | |
| 638 | func expandValues(values ...any) (results []any) { |
| 639 | appendToResult := func(rv reflect.Value) { |