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

Commit 3ca94230 by Sascha Depold

fixed some issues on belongsTo

1 parent 5c1ea45c
...@@ -14,8 +14,8 @@ module.exports.Factory = function(Sequelize, sequelize) { ...@@ -14,8 +14,8 @@ module.exports.Factory = function(Sequelize, sequelize) {
}, },
addOneToOneMethods: function(table1, table2, assocName, backAssocName) { addOneToOneMethods: function(table1, table2, assocName, backAssocName) {
var setterName = Sequelize.Helper.SQL.addPrefix('set', backAssocName || assocName), var setterName = Sequelize.Helper.SQL.addPrefix('set', backAssocName || assocName, true),
getterName = Sequelize.Helper.SQL.addPrefix('get', backAssocName || assocName) getterName = Sequelize.Helper.SQL.addPrefix('get', backAssocName || assocName, true)
// getter // getter
table1.prototype[getterName] = function(callback) { table1.prototype[getterName] = function(callback) {
...@@ -28,7 +28,7 @@ module.exports.Factory = function(Sequelize, sequelize) { ...@@ -28,7 +28,7 @@ module.exports.Factory = function(Sequelize, sequelize) {
table1.prototype[setterName] = function(object, callback) { table1.prototype[setterName] = function(object, callback) {
var self = this var self = this
this[Sequelize.Helper.SQL.addPrefix('get', backAssocName)](function(currentAssociation) { this[Sequelize.Helper.SQL.addPrefix('get', backAssocName, true)](function(currentAssociation) {
var attr = {} var attr = {}
if(currentAssociation == null) { if(currentAssociation == null) {
......
...@@ -205,20 +205,19 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -205,20 +205,19 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
throw new Error("Calling belongsTo with only two parameters is deprecated! Please take a look at the example in the repository!") throw new Error("Calling belongsTo with only two parameters is deprecated! Please take a look at the example in the repository!")
// start - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys // start - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys
var Factory = new require("./Factory").Factory(Sequelize, sequelize) var Factory = new require("./Factory").Factory(Sequelize, sequelize),
isManyToManyAssociation = Sequelize.Helper.SQL.isManyToManyAssociation(backAssociation)
if(backAssociation.type == 'hasMany') { delete _table.prototype[Sequelize.Helper.SQL.addPrefix('get', backAssociation.name, !isManyToManyAssociation)]
delete _table.prototype[Sequelize.Helper.SQL.addPrefix('get', backAssociation.name)] delete _table.prototype[Sequelize.Helper.SQL.addPrefix('set', backAssociation.name, !isManyToManyAssociation)]
delete _table.prototype[Sequelize.Helper.SQL.addPrefix('set', backAssociation.name)]
if(backAssociation.type == 'hasMany')
Factory.addOneToManyMethods(_table, table, assocName, backAssociation.name) Factory.addOneToManyMethods(_table, table, assocName, backAssociation.name)
} else { else
delete _table.prototype[Sequelize.Helper.SQL.addPrefix('get', backAssociation.name)]
delete _table.prototype[Sequelize.Helper.SQL.addPrefix('set', backAssociation.name)]
Factory.addOneToOneMethods(_table, table, assocName, backAssociation.name) Factory.addOneToOneMethods(_table, table, assocName, backAssociation.name)
}
// TODO: check if the following line is not needed; specs r failing // TODO: check if the following line is not needed; specs r failing
// backAssociation.name = assocName // backAssociation.name = assocName
// end - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys // 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' }) table.associations.push({ name: assocName, table: _table, type: 'belongsTo' })
......
...@@ -69,7 +69,8 @@ module.exports = { ...@@ -69,7 +69,8 @@ module.exports = {
assert.equal(h.SQL.hashToWhereConditions({name: 'asd'}, Day.attributes), "name='asd'") assert.equal(h.SQL.hashToWhereConditions({name: 'asd'}, Day.attributes), "name='asd'")
}, },
'SQL: addPrefix': function(assert) { 'SQL: addPrefix': function(assert) {
assert.equal(h.SQL.addPrefix('foo', 'bar'), 'fooBar') assert.equal(h.SQL.addPrefix('foo', 'bar', true), 'fooBar')
assert.equal(h.SQL.addPrefix('foo', 'bar', false), 'fooBars')
}, },
'Hash: forEach': function(assert) { 'Hash: forEach': function(assert) {
var values = [] var values = []
......
...@@ -158,15 +158,15 @@ module.exports = { ...@@ -158,15 +158,15 @@ module.exports = {
var Day = s.define('Day2', { name: Sequelize.TEXT }) var Day = s.define('Day2', { name: Sequelize.TEXT })
var HasOneBlubb = s.define('HasOneBlubb', {}) var HasOneBlubb = s.define('HasOneBlubb', {})
Day.hasOne('HasOneBlubb', HasOneBlubb) Day.hasOne('HasOneBlubb', HasOneBlubb)
assert.isDefined(new Day({name:''}).getHasOneBlubb)
assert.isDefined(Day.prototype.getHasOneBlubb)
}, },
'hasOne set association': function(assert, beforeExit) { 'hasOne set association': function(assert, beforeExit) {
var s2 = new Sequelize('sequelize_test', 'root', null, {disableLogging: true}) var s2 = new Sequelize('sequelize_test', 'root', null, {disableLogging: true})
var Task = s2.define('Task', {title: Sequelize.STRING}) var Task = s2.define('Task', {title: Sequelize.STRING})
var Deadline = s2.define('Deadline', {date: Sequelize.DATE}) var Deadline = s2.define('Deadline', {date: Sequelize.DATE})
var taskAssoc = Task.hasOne('deadline', Deadline) Task.hasOneAndBelongsTo('deadline', Deadline, 'task')
Deadline.belongsTo('task', Task, taskAssoc)
var task = new Task({ title:'do smth' }) var task = new Task({ title:'do smth' })
var deadline = new Deadline({date: new Date()}) var deadline = new Deadline({date: new Date()})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!