Refactor named value support for PolymorphicType

This commit is contained in:
Jinzhu 2016-10-06 20:33:48 +08:00
parent 1413e55339
commit afaadc3942
5 changed files with 18 additions and 34 deletions

View File

@ -63,11 +63,7 @@ func (association *Association) Replace(values ...interface{}) *Association {
} else {
// Polymorphic Relations
if relationship.PolymorphicDBName != "" {
value := scope.TableName()
if relationship.PolymorphicValue != "" {
value = relationship.PolymorphicValue
}
newDB = newDB.Where(fmt.Sprintf("%v = ?", scope.Quote(relationship.PolymorphicDBName)), value)
newDB = newDB.Where(fmt.Sprintf("%v = ?", scope.Quote(relationship.PolymorphicDBName)), relationship.PolymorphicValue)
}
// Delete Relations except new created
@ -286,13 +282,9 @@ func (association *Association) Count() int {
}
if relationship.PolymorphicType != "" {
value := scope.TableName()
if relationship.PolymorphicValue != "" {
value = relationship.PolymorphicValue
}
query = query.Where(
fmt.Sprintf("%v.%v = ?", scope.New(fieldValue).QuotedTableName(), scope.Quote(relationship.PolymorphicDBName)),
value,
relationship.PolymorphicValue,
)
}

View File

@ -114,11 +114,7 @@ func (scope *Scope) handleHasOnePreload(field *Field, conditions []interface{})
values := toQueryValues(primaryKeys)
if relation.PolymorphicType != "" {
query += fmt.Sprintf(" AND %v = ?", scope.Quote(relation.PolymorphicDBName))
value := scope.TableName()
if relation.PolymorphicValue != "" {
value = relation.PolymorphicValue
}
values = append(values, value)
values = append(values, relation.PolymorphicValue)
}
results := makeSlice(field.Struct.Type)
@ -167,11 +163,7 @@ func (scope *Scope) handleHasManyPreload(field *Field, conditions []interface{})
values := toQueryValues(primaryKeys)
if relation.PolymorphicType != "" {
query += fmt.Sprintf(" AND %v = ?", scope.Quote(relation.PolymorphicDBName))
value := scope.TableName()
if relation.PolymorphicValue != "" {
value = relation.PolymorphicValue
}
values = append(values, value)
values = append(values, relation.PolymorphicValue)
}
results := makeSlice(field.Struct.Type)

View File

@ -60,11 +60,7 @@ func saveAfterAssociationsCallback(scope *Scope) {
}
if relationship.PolymorphicType != "" {
value := scope.TableName()
if relationship.PolymorphicValue != "" {
value = relationship.PolymorphicValue
}
scope.Err(newScope.SetColumn(relationship.PolymorphicType, value))
scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue))
}
scope.Err(newDB.Save(elem).Error)
@ -86,11 +82,7 @@ func saveAfterAssociationsCallback(scope *Scope) {
}
if relationship.PolymorphicType != "" {
value := scope.TableName()
if relationship.PolymorphicValue != "" {
value = relationship.PolymorphicValue
}
scope.Err(newScope.SetColumn(relationship.PolymorphicType, value))
scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue))
}
scope.Err(scope.NewDB().Save(elem).Error)
}

View File

@ -294,7 +294,11 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
relationship.PolymorphicType = polymorphicType.Name
relationship.PolymorphicDBName = polymorphicType.DBName
// if Dog has multiple set of toys set name of the set (instead of default 'dogs')
relationship.PolymorphicValue = field.TagSettings["VALUE"]
if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok {
relationship.PolymorphicValue = value
} else {
relationship.PolymorphicValue = scope.TableName()
}
polymorphicType.IsForeignKey = true
}
}
@ -388,7 +392,11 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
relationship.PolymorphicType = polymorphicType.Name
relationship.PolymorphicDBName = polymorphicType.DBName
// if Cat has several different types of toys set name for each (instead of default 'cats')
relationship.PolymorphicValue = field.TagSettings["VALUE"]
if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok {
relationship.PolymorphicValue = value
} else {
relationship.PolymorphicValue = scope.TableName()
}
polymorphicType.IsForeignKey = true
}
}

View File

@ -21,8 +21,8 @@ type Dog struct {
type Hamster struct {
Id int
Name string
PreferredToy Toy `gorm:"polymorphic:Owner;value:hamster_preferred"`
OtherToy Toy `gorm:"polymorphic:Owner;value:hamster_other"`
PreferredToy Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_preferred"`
OtherToy Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_other"`
}
type Toy struct {