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

Commit c7592d70 by Sascha Depold

added method for fetching associated data

1 parent f5c504a6
......@@ -14,6 +14,10 @@ exports.Helper = function(Sequelize) {
},
SQL: {
isManyToManyAssociation: function(association) {
return (['hasMany', 'hasAndBelongsToMany'].indexOf(association.type) > -1)
},
manyToManyTableName: function(name1, name2) {
var _name1 = name1[0].toUpperCase() + name1.replace(/^./, "")
var _name2 = name2[0].toUpperCase() + name2.replace(/^./, "")
......
......@@ -217,7 +217,8 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
Factory.addOneToOneMethods(_table, table, assocName, backAssociation.name)
}
backAssociation.name = assocName
// TODO: check if the following line is not needed; specs r failing
// backAssociation.name = assocName
// end - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys
table.associations.push({ name: assocName, table: _table, type: 'belongsTo' })
......@@ -286,6 +287,29 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
return this.id == null
},
loadAssociatedData: function(callback) {
var associatedData = {},
self = this,
setAssociatedDataAndReturn = function() {
self.associatedData = associatedData
if(callback) callback(associatedData)
}
if(this.table.associations.length == 0)
setAssociatedDataAndReturn()
else
this.table.associations.forEach(function(association) {
var isManyToManyAssociation = Sequelize.Helper.SQL.isManyToManyAssociation(association),
getter = Sequelize.Helper.SQL.addPrefix('get', association.name, !isManyToManyAssociation)
self[getter](function(objects) {
associatedData[association.name] = objects
if(Sequelize.Helper.Hash.keys(associatedData).length == self.table.associations.length)
setAssociatedDataAndReturn()
})
})
},
save: function(callback) {
var query = null,
self = this
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!