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

Commit 4d39a3bd by Sascha Depold

hasAndBelongsToMany association!

1 parent 14b399db
var Sequelize = require(__dirname + "/../../lib/sequelize/Sequelize").Sequelize,
sequelize = new Sequelize("sequelize_test", "root", null)
sequelize = new Sequelize("sequelize_test", "root", null, {disableLogging: true})
var Person = sequelize.define('person', {
name: Sequelize.STRING
......@@ -9,8 +9,8 @@ var Pet = sequelize.define('pet', {
name: Sequelize.STRING
})
/*Person.hasMany('brothers', Person)
Person.hasMany('sisters', Person)*/
Person.hasMany('brothers')
Person.hasMany('sisters')
var fatherAssoc = Person.hasOne('father', Person)
var motherAssoc = Person.hasOne('mother', Person)
......@@ -31,8 +31,8 @@ Sequelize.chainQueries([{drop: sequelize}, {sync: sequelize}], function() {
Sequelize.chainQueries([{save: person}, {save: mother}, {save: father}, {save: brother}, {save: sister}, {save: pet}], function() {
person.setMother(mother, function(mom) { Sequelize.Helper.log('my mom: ' + mom.name) })
person.setFather(father, function(dad) { Sequelize.Helper.log('my dad: ' + dad.name) })
/*person.setBrothers([brother], function(bros) { Sequelize.Helper.log('ma bro: ' + bros[0].name)})
person.setSisters([sister], function(sis) { Sequelize.Helper.log('ma sis\': ' + sis[0].name)})*/
person.setBrothers([brother], function(bros) { Sequelize.Helper.log("ma bro: " + bros[0].name)})
person.setSisters([sister], function(sis) { Sequelize.Helper.log("ma sis: " + sis[0].name)})
person.setPets([pet], function(pets) { Sequelize.Helper.log('my pet: ' + pets[0].name )})
})
})
\ No newline at end of file
......@@ -133,7 +133,6 @@ module.exports.Factory = function(Sequelize, sequelize) {
return function(objects, callback) {
var self = this,
objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id })
Sequelize.Helper.log(this)
this[Sequelize.Helper.SQL.addPrefix('get', methodName)](function(currentAssociations) {
var currentIds = Sequelize.Helper.Array.map(currentAssociations, function(assoc) { return assoc.id }),
......
......@@ -61,6 +61,12 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
association.table.attributes[assocNameAsTableIdentifier] = {type: Sequelize.INTEGER}
}
break
case 'hasAndBelongsToMany':
var _attributes = {}
_attributes[assocNameAsTableIdentifier] = Sequelize.INTEGER
_attributes[Sequelize.Helper.SQL.asTableIdentifier(association.table.tableName)] = Sequelize.INTEGER
sequelize.define(Sequelize.Helper.SQL.manyToManyTableName(association.name, association.table.tableName), _attributes)
break
case 'hasOne':
// adds the foreign key to the associated table
// e.g. assocTable.myTableId = Sequelize.INTEGER
......@@ -134,20 +140,42 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
return object
},
hasMany: function(assocName, _table, backAssocName) {
hasAndBelongsToMany: function(assocName) {
if(typeof assocName == 'undefined')
throw new Error('Please specify at least an association name!')
var association = { name: assocName, table: table, type: 'hasAndBelongsToMany' },
Factory = new require("./Factory").Factory(Sequelize, sequelize)
table.associations.push(association)
table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = Factory.createManyToManyGetter(null, table, assocName, table.tableName)
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createManyToManySetter(table, table, assocName, table.tableName)
return association
},
hasMany: function(assocName, associateWith, backAssocName) {
if(typeof assocName == 'undefined')
throw new Error('Please specify at least an association name!')
if(associateWith) {
var Factory = new require("./Factory").Factory(Sequelize, sequelize),
association = { name: assocName, backAssociationName: backAssocName, table: _table, type: 'hasMany' }
association = { name: assocName, backAssociationName: backAssocName, table: associateWith, type: 'hasMany' }
table.associations.push(association)
if(backAssocName) {
Factory.addManyToManyMethods(table, _table, assocName, backAssocName)
Factory.addManyToManyMethods(_table, table, backAssocName, assocName)
Factory.addManyToManyMethods(table, associateWith, assocName, backAssocName)
Factory.addManyToManyMethods(associateWith, table, backAssocName, assocName)
} else {
Factory.addOneToManyMethods(table, _table, assocName)
Factory.addOneToManyMethods(table, associateWith, assocName)
}
return association
} else {
return this.hasAndBelongsToMany(assocName)
}
},
hasOne: function(assocName, _table) {
......@@ -242,6 +270,10 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
return this.invalidFields.length == 0
},
get isNewRecord() {
return this.id == null
},
save: function(callback) {
var query = null,
self = this
......@@ -252,7 +284,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
}
this.updatedAt = new Date()
if(this.id == null) {
if(this.isNewRecord) {
this.createdAt = new Date()
query = Sequelize.sqlQueryFor('insert', {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!