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

Commit 5162c970 by Sascha Depold

first steps for getting one to many assocs run again

1 parent d23453be
Showing with 23 additions and 20 deletions
......@@ -136,13 +136,9 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
hasMany: function(assocName, _table, backAssocName) {
var Factory = new require("./Factory").Factory(Sequelize, sequelize)
var association = { name: assocName, backAssociationName: backAssocName, table: _table, type: 'hasMany' }
table.associations.push({
name: assocName,
backAssociationName: backAssocName,
table: _table,
type: 'hasMany'
})
table.associations.push(association)
// don't check inside of method to increase performance
if(backAssocName) {
......@@ -155,7 +151,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createOneToManySetter(table, assocName)
}
return table
return association
},
hasOne: function(assocName, _table) {
......@@ -195,23 +191,30 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
return table
},
belongsTo: function(assocName, _table) {
table.associations.push({
name: assocName,
table: _table,
type: 'belongsTo'
})
belongsTo: function(assocName, _table, backAssociation) {
if(typeof backAssociation == 'undefined')
throw new Error("Calling belongsTo with only two parameters is deprecated! Please take a look at the example in the repository!")
// this will overwrite the association name of the before defined hasOne or hasMany relation
backAssociation.name = assocName
table.associations.push({ name: assocName, table: _table, type: 'belongsTo' })
// getter
table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = function(callback) {
if((this[_table.identifier] == null)||(isNaN(this[_table.identifier])))
callback([])
else
_table.find(this[_table.identifier], callback)
var identifier = Sequelize.Helper.SQL.asTableIdentifier(assocName)
if((this[identifier] == null)||(isNaN(this[identifier]))) callback([])
else _table.find(this[identifier], callback)
}
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = function(object, callback) {
var attr = {}; attr[object.table.identifier] = object.id
var self = this
// setter
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName, true)] = function(object, callback) {
var attr = {},
self = this
attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = object.id
this.updateAttributes(attr, function() {
self[Sequelize.Helper.SQL.addPrefix('get', assocName)](callback)
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!