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

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 ...@@ -136,13 +136,9 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
hasMany: function(assocName, _table, backAssocName) { hasMany: function(assocName, _table, backAssocName) {
var Factory = new require("./Factory").Factory(Sequelize, sequelize) var Factory = new require("./Factory").Factory(Sequelize, sequelize)
var association = { name: assocName, backAssociationName: backAssocName, table: _table, type: 'hasMany' }
table.associations.push({ table.associations.push(association)
name: assocName,
backAssociationName: backAssocName,
table: _table,
type: 'hasMany'
})
// don't check inside of method to increase performance // don't check inside of method to increase performance
if(backAssocName) { if(backAssocName) {
...@@ -155,7 +151,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -155,7 +151,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createOneToManySetter(table, assocName) table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createOneToManySetter(table, assocName)
} }
return table return association
}, },
hasOne: function(assocName, _table) { hasOne: function(assocName, _table) {
...@@ -195,23 +191,30 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -195,23 +191,30 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
return table return table
}, },
belongsTo: function(assocName, _table) { belongsTo: function(assocName, _table, backAssociation) {
table.associations.push({ if(typeof backAssociation == 'undefined')
name: assocName, throw new Error("Calling belongsTo with only two parameters is deprecated! Please take a look at the example in the repository!")
table: _table,
type: 'belongsTo' // 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) { table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = function(callback) {
if((this[_table.identifier] == null)||(isNaN(this[_table.identifier]))) var identifier = Sequelize.Helper.SQL.asTableIdentifier(assocName)
callback([])
else if((this[identifier] == null)||(isNaN(this[identifier]))) callback([])
_table.find(this[_table.identifier], callback) else _table.find(this[identifier], callback)
} }
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = function(object, callback) { // setter
var attr = {}; attr[object.table.identifier] = object.id table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName, true)] = function(object, callback) {
var self = this var attr = {},
self = this
attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = object.id
this.updateAttributes(attr, function() { this.updateAttributes(attr, function() {
self[Sequelize.Helper.SQL.addPrefix('get', assocName)](callback) 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!