不要怂,就是干,撸起袖子干!

Commit 29efe954 by Sascha Depold

1-to-N association setting

1 parent 4fda57ac
Showing with 20 additions and 2 deletions
......@@ -136,7 +136,6 @@ SequelizeTable = function(sequelize, tableName, attributes) {
if(result.length > 0) {
var ids = []
result.forEach(function(resultSet) { ids.push(resultSet.id) })
_table.findAll({where: "id IN (" + ids.join(",") + ")"}, callback)
} else {
if(callback) callback([])
......@@ -170,7 +169,7 @@ SequelizeTable = function(sequelize, tableName, attributes) {
}
var createNewAssociations = function(obsolete) {
var currentIds = SequelizeHelper.Array.map(currentAssociations, function(assoc) { return assoc.id })
var withoutExisting = SequelizeHelper.Array.without(objects, function(o) {
var withoutExisting = SequelizeHelper.Array.reject(objects, function(o) {
currentIds.indexOf(o.id) > -1
})
var savings = []
......@@ -198,6 +197,25 @@ SequelizeTable = function(sequelize, tableName, attributes) {
var whereConditions = [table.identifier, this.id].join("=")
_table.findAll({where: whereConditions}, callback)
}
table.prototype[SequelizeHelper.SQL.addPrefix('set', assocName)] = function(objects, callback) {
var self = this
var objectIds = SequelizeHelper.Array.map(objects, function(obj) { return obj.id })
this[assocName](function(currentAssociations) {
var currentIds = SequelizeHelper.Array.map(currentAssociations, function(assoc) { return assoc.id })
var obsoleteAssociations = SequelizeHelper.Array.select(currentAssociations, function(assoc) { return objectsIds.indexOf(assoc.id) == -1 })
obsoleteAssociations.forEach(function(assoc) {
var attr = {}; attr[table.identifier] = null
assoc.updateAttributes(attr)
})
var newAssociations = SequelizeHelper.Array.select(objects, function(o) { return currentIds.indexOf(o.id) == -1 })
newAssociations.forEach(function(assoc) {
var attr = {}; attr[table.identifier] = self.id
assoc.updateAttributes(attr)
})
self[assocName](callback)
})
}
}
return table
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!