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

Commit 78848c5b by Sascha Depold

fixed self associated has-one associations

1 parent b98d94f4
...@@ -5,6 +5,12 @@ var HasOne = module.exports = function(srcModel, targetModel, options) { ...@@ -5,6 +5,12 @@ var HasOne = module.exports = function(srcModel, targetModel, options) {
this.source = srcModel this.source = srcModel
this.target = targetModel this.target = targetModel
this.options = options this.options = options
this.isSelfAssociation = (this.source.tableName == this.target.tableName)
this.associationAccessor = this.isSelfAssociation
? Utils.combineTableNames(this.target.tableName, this.options.as || this.target.tableName)
: this.target.tableName
this.accessors = { this.accessors = {
get: Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName))), get: Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName))),
set: Utils._.camelize('set_' + (this.options.as || Utils.singularize(this.target.tableName))) set: Utils._.camelize('set_' + (this.options.as || Utils.singularize(this.target.tableName)))
...@@ -49,7 +55,7 @@ HasOne.prototype.injectSetter = function(obj) { ...@@ -49,7 +55,7 @@ HasOne.prototype.injectSetter = function(obj) {
associatedObject[self.identifier] = obj.id associatedObject[self.identifier] = obj.id
associatedObject.save() associatedObject.save()
.on('success', function() { customEventEmitter.emit('success', '') }) .on('success', function() { customEventEmitter.emit('success', associatedObject) })
.on('failure', function(err) { customEventEmitter.emit('failure', err) }) .on('failure', function(err) { customEventEmitter.emit('failure', err) })
}) })
}) })
......
...@@ -9,7 +9,7 @@ var Associations = module.exports = { ...@@ -9,7 +9,7 @@ var Associations = module.exports = {
// the id is in the foreign table // the id is in the foreign table
var HasOne = require('./has-one') var HasOne = require('./has-one')
var association = new HasOne(this, associatedModel, Utils._.extend((options||{}), this.options)) var association = new HasOne(this, associatedModel, Utils._.extend((options||{}), this.options))
this.associations[associatedModel.tableName] = association.injectAttributes() this.associations[association.associationAccessor] = association.injectAttributes()
}, },
belongsTo: function(associatedModel, options) { belongsTo: function(associatedModel, options) {
// the id is in this table // the id is in this table
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!