github.com/Accefy/pop@v0.0.0-20230428174248-e9f677eab5b9/belongs_to.go (about) 1 package pop 2 3 import ( 4 "fmt" 5 ) 6 7 // BelongsTo adds a "where" clause based on the "ID" of the 8 // "model" passed into it. 9 func (c *Connection) BelongsTo(model interface{}) *Query { 10 return Q(c).BelongsTo(model) 11 } 12 13 // BelongsToAs adds a "where" clause based on the "ID" of the 14 // "model" passed into it using an alias. 15 func (c *Connection) BelongsToAs(model interface{}, as string) *Query { 16 return Q(c).BelongsToAs(model, as) 17 } 18 19 // BelongsTo adds a "where" clause based on the "ID" of the 20 // "model" passed into it. 21 func (q *Query) BelongsTo(model interface{}) *Query { 22 m := NewModel(model, q.Connection.Context()) 23 q.Where(fmt.Sprintf("%s = ?", m.associationName()), m.ID()) 24 return q 25 } 26 27 // BelongsToAs adds a "where" clause based on the "ID" of the 28 // "model" passed into it, using an alias. 29 func (q *Query) BelongsToAs(model interface{}, as string) *Query { 30 m := NewModel(model, q.Connection.Context()) 31 q.Where(fmt.Sprintf("%s = ?", as), m.ID()) 32 return q 33 } 34 35 // BelongsToThrough adds a "where" clause that connects the "bt" model 36 // through the associated "thru" model. 37 func (c *Connection) BelongsToThrough(bt, thru interface{}) *Query { 38 return Q(c).BelongsToThrough(bt, thru) 39 } 40 41 // BelongsToThrough adds a "where" clause that connects the "bt" model 42 // through the associated "thru" model. 43 func (q *Query) BelongsToThrough(bt, thru interface{}) *Query { 44 q.belongsToThroughClauses = append(q.belongsToThroughClauses, belongsToThroughClause{ 45 BelongsTo: NewModel(bt, q.Connection.Context()), 46 Through: NewModel(thru, q.Connection.Context()), 47 }) 48 return q 49 }