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

Commit fcabb59e by Sascha Depold

use the association names for generating the many-to-many-tables

1 parent 155381bd
......@@ -2,7 +2,7 @@ module.exports.Factory = function(Sequelize, sequelize) {
return {
createManyToManyGetter: function(table1, table2, assocName, backAssocName) {
return function(callback) {
var Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, table1.tableName)].klass
var Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, backAssocName)].klass
var whereConditions = [table1.identifier, this.id].join("=")
Association.findAll({ where: whereConditions }, function(result) {
if(result.length > 0) {
......@@ -18,7 +18,7 @@ module.exports.Factory = function(Sequelize, sequelize) {
createManyToManySetter: function(table1, table2, assocName, backAssocName) {
return function(objects, callback) {
var self = this,
Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, table1.tableName)].klass,
Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, backAssocName)].klass,
currentAssociations = null,
objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id })
......
......@@ -67,12 +67,12 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
table.associations.forEach(function(association) {
switch(association.type) {
case 'hasMany':
if(association.table.isCrossAssociatedWith(table)) {
if(association.backAssociationName) {
// many to many relation
var _attributes = {}
_attributes[table.identifier] = Sequelize.INTEGER
_attributes[association.table.identifier] = Sequelize.INTEGER
sequelize.define(Sequelize.Helper.SQL.manyToManyTableName(table.tableName, association.name), _attributes)
sequelize.define(Sequelize.Helper.SQL.manyToManyTableName(association.name, association.backAssociationName), _attributes)
} else {
// one to many relation
association.table.attributes[table.identifier] = {type: Sequelize.INTEGER}
......@@ -149,19 +149,22 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
return object
},
hasMany: function(assocName, _table) {
hasMany: function(assocName, _table, backAssocName) {
var Factory = new require("./Factory").Factory(Sequelize, sequelize)
table.associations.push({
name: assocName,
backAssociationName: backAssocName,
table: _table,
type: 'hasMany'
})
// don't check inside of method to increase performance
if(_table.isCrossAssociatedWith(table)) {
table.prototype[assocName] = Factory.createManyToManyGetter(table, _table, assocName)
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createManyToManySetter(table, _table, assocName)
if(backAssocName) {
table.prototype[assocName] = Factory.createManyToManyGetter(table, _table, assocName, backAssocName)
_table.prototype[backAssocName] = Factory.createManyToManyGetter(_table, table, backAssocName, assocName)
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createManyToManySetter(table, _table, assocName, backAssocName)
_table.prototype[Sequelize.Helper.SQL.addPrefix('set', backAssocName)] = Factory.createManyToManySetter(_table, table, backAssocName, assocName)
} else {
table.prototype[assocName] = Factory.createOneToManyGetter(table, _table)
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createOneToManySetter(table)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!