I øjeblikket giver GORM for MongoDB ikke korrekte værdier i dirtyPropertyNames
Mark. Så du skal bruge et andet injiceret felt på lavere niveau i domæneforekomsten, dvs. $changedProperties
.
Men der er også et problem med $changedProperties
at selvom du binder et felt med samme værdi, vil $changedProperties
vil have en post til det. Så du kan justere den lidt mere sådan her for at få din kode til at virke:
def beforeUpdate() {
def instance = this
Map updatedFields = instance.$changedProperties
updatedFields.each { name, value ->
if (updatedFields[name] != instance[name]) {
println "Field value $name is updated"
if (name == "addresses") {
// I've not run this for a long time, just confirm the old and new addresses values and swap the assignment of below lines
List newAddresses = updatedFields[name]
List oldAddresses = instance[name]
newAddresses.each { address ->
if (!address.id) {
println "Got new address: $address.status"
} else {
Address oldAddress = oldAddresses.find { it.id == address.id }
if (!oldAddress) { // This is just an edge condition
println "Got new address: $address.status"
} else if (oldAddress.status != address.staus) {
println "$address status is updated to $address.status"
}
}
}
}
}
}
}