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

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) { ...@@ -9,7 +9,7 @@ module.exports.Factory = function(Sequelize, sequelize) {
var setterName = Sequelize.Helper.SQL.addPrefix('set', methodName || assocName), var setterName = Sequelize.Helper.SQL.addPrefix('set', methodName || assocName),
getterName = Sequelize.Helper.SQL.addPrefix('get', 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) table1.prototype[getterName] = Factory.createOneToManyGetter(table1, table2, assocName)
}, },
...@@ -129,12 +129,13 @@ module.exports.Factory = function(Sequelize, sequelize) { ...@@ -129,12 +129,13 @@ module.exports.Factory = function(Sequelize, sequelize) {
table2.findAll({where: whereConditions}, callback) table2.findAll({where: whereConditions}, callback)
} }
}, },
createOneToManySetter: function(table1) { createOneToManySetter: function(table1, assocName, methodName) {
return function(objects, callback) { return function(objects, callback) {
var self = this, var self = this,
objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id }) 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 }), 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 }), obsoleteAssociations = Sequelize.Helper.Array.select(currentAssociations, function(assoc) { return objectsIds.indexOf(assoc.id) == -1 }),
queries = [] queries = []
...@@ -153,7 +154,7 @@ module.exports.Factory = function(Sequelize, sequelize) { ...@@ -153,7 +154,7 @@ module.exports.Factory = function(Sequelize, sequelize) {
}) })
Sequelize.chainQueries(queries, function() { 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!