forked from mirror/gorm
Use new relationship API
This commit is contained in:
parent
fcb6b06d22
commit
2a8c7973cb
|
@ -98,8 +98,8 @@ func (association *Association) Delete(values ...interface{}) *Association {
|
||||||
// many to many
|
// many to many
|
||||||
if relationship.Kind == "many_to_many" {
|
if relationship.Kind == "many_to_many" {
|
||||||
whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v IN (?)",
|
whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v IN (?)",
|
||||||
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.ForeignFieldName)),
|
relationship.JoinTable, association.Scope.Quote(relationship.ForeignDBName),
|
||||||
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignFieldName)))
|
relationship.JoinTable, association.Scope.Quote(relationship.AssociationForeignDBName))
|
||||||
|
|
||||||
association.Scope.db.Model("").Table(relationship.JoinTable).
|
association.Scope.db.Model("").Table(relationship.JoinTable).
|
||||||
Where(whereSql, association.PrimaryKey, primaryKeys).Delete("")
|
Where(whereSql, association.PrimaryKey, primaryKeys).Delete("")
|
||||||
|
@ -138,8 +138,8 @@ func (association *Association) Replace(values ...interface{}) *Association {
|
||||||
}
|
}
|
||||||
|
|
||||||
whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v NOT IN (?)",
|
whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v NOT IN (?)",
|
||||||
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.ForeignFieldName)),
|
relationship.JoinTable, association.Scope.Quote(relationship.ForeignDBName),
|
||||||
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignFieldName)))
|
relationship.JoinTable, association.Scope.Quote(relationship.AssociationForeignDBName))
|
||||||
|
|
||||||
scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, association.PrimaryKey, addedPrimaryKeys).Delete("")
|
scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, association.PrimaryKey, addedPrimaryKeys).Delete("")
|
||||||
} else {
|
} else {
|
||||||
|
@ -152,7 +152,7 @@ func (association *Association) Clear() *Association {
|
||||||
relationship := association.Field.Relationship
|
relationship := association.Field.Relationship
|
||||||
scope := association.Scope
|
scope := association.Scope
|
||||||
if relationship.Kind == "many_to_many" {
|
if relationship.Kind == "many_to_many" {
|
||||||
whereSql := fmt.Sprintf("%v.%v = ?", relationship.JoinTable, scope.Quote(ToSnake(relationship.ForeignFieldName)))
|
whereSql := fmt.Sprintf("%v.%v = ?", relationship.JoinTable, scope.Quote(relationship.ForeignDBName))
|
||||||
scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, association.PrimaryKey).Delete("")
|
scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, association.PrimaryKey).Delete("")
|
||||||
} else {
|
} else {
|
||||||
association.setErr(errors.New("clear only support many to many"))
|
association.setErr(errors.New("clear only support many to many"))
|
||||||
|
@ -173,13 +173,13 @@ func (association *Association) Count() int {
|
||||||
newScope.QuotedTableName(),
|
newScope.QuotedTableName(),
|
||||||
scope.Quote(newScope.PrimaryKey()),
|
scope.Quote(newScope.PrimaryKey()),
|
||||||
relationship.JoinTable,
|
relationship.JoinTable,
|
||||||
scope.Quote(ToSnake(relationship.AssociationForeignFieldName)),
|
scope.Quote(relationship.AssociationForeignDBName),
|
||||||
relationship.JoinTable,
|
relationship.JoinTable,
|
||||||
relationship.JoinTable,
|
relationship.JoinTable,
|
||||||
scope.Quote(ToSnake(relationship.ForeignFieldName)))
|
scope.Quote(relationship.ForeignDBName))
|
||||||
scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey).Count(&count)
|
scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey).Count(&count)
|
||||||
} else if relationship.Kind == "has_many" || relationship.Kind == "has_one" {
|
} else if relationship.Kind == "has_many" || relationship.Kind == "has_one" {
|
||||||
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignFieldName)))
|
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(relationship.ForeignDBName))
|
||||||
countScope := scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey)
|
countScope := scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey)
|
||||||
if relationship.ForeignType != "" {
|
if relationship.ForeignType != "" {
|
||||||
countScope = countScope.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignType))), association.PrimaryType)
|
countScope = countScope.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignType))), association.PrimaryType)
|
||||||
|
@ -187,7 +187,7 @@ func (association *Association) Count() int {
|
||||||
countScope.Count(&count)
|
countScope.Count(&count)
|
||||||
} else if relationship.Kind == "belongs_to" {
|
} else if relationship.Kind == "belongs_to" {
|
||||||
if v, err := scope.FieldValueByName(association.Column); err == nil {
|
if v, err := scope.FieldValueByName(association.Column); err == nil {
|
||||||
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignFieldName)))
|
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(relationship.ForeignDBName))
|
||||||
scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, v).Count(&count)
|
scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, v).Count(&count)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue