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

Commit 14b399db by Sascha Depold

fix in one-to-many setter

1 parent fff633bb
Showing with 5 additions and 4 deletions
......@@ -9,7 +9,7 @@ module.exports.Factory = function(Sequelize, sequelize) {
var setterName = Sequelize.Helper.SQL.addPrefix('set', methodName || assocName),
getterName = Sequelize.Helper.SQL.addPrefix('get', methodName || assocName)
table1.prototype[setterName] = Factory.createOneToManySetter(table1, assocName)
table1.prototype[setterName] = Factory.createOneToManySetter(table1, assocName, methodName)
table1.prototype[getterName] = Factory.createOneToManyGetter(table1, table2, assocName)
},
......@@ -129,12 +129,13 @@ module.exports.Factory = function(Sequelize, sequelize) {
table2.findAll({where: whereConditions}, callback)
}
},
createOneToManySetter: function(table1) {
createOneToManySetter: function(table1, assocName, methodName) {
return function(objects, callback) {
var self = this,
objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id })
Sequelize.Helper.log(this)
this[assocName](function(currentAssociations) {
this[Sequelize.Helper.SQL.addPrefix('get', methodName)](function(currentAssociations) {
var currentIds = Sequelize.Helper.Array.map(currentAssociations, function(assoc) { return assoc.id }),
obsoleteAssociations = Sequelize.Helper.Array.select(currentAssociations, function(assoc) { return objectsIds.indexOf(assoc.id) == -1 }),
queries = []
......@@ -153,7 +154,7 @@ module.exports.Factory = function(Sequelize, sequelize) {
})
Sequelize.chainQueries(queries, function() {
self[assocName](callback)
self[Sequelize.Helper.SQL.addPrefix('get', methodName)](callback)
})
})
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!