Skip to content

Commit

Permalink
Ensure hasMany() associations work when properties have mapsTo
Browse files Browse the repository at this point in the history
  • Loading branch information
stueynz committed Dec 1, 2015
1 parent 2069884 commit 088cd9f
Showing 2 changed files with 691 additions and 5 deletions.
30 changes: 25 additions & 5 deletions lib/Associations/Many.js
Original file line number Diff line number Diff line change
@@ -149,9 +149,19 @@ function extendInstance(Model, Instance, Driver, association, opts, createInstan

options.__merge = {
from: { table: association.mergeTable, field: Object.keys(association.mergeAssocId) },
to: { table: association.model.table, field: association.model.id },
to: { table: association.model.table, field: association.model.id.slice(0) }, // clone model id
where: [ association.mergeTable, {} ]
};

// Loop through the (cloned) association model id fields ... some of them may've been mapped to different
// names in the actual database - if so update to the mapped database column name
for(i=0; i<options.__merge.to.field.length; i++) {
var idProp = association.model.properties[options.__merge.to.field[i]];
if(idProp && idProp.mapsTo) {
options.__merge.to.field[i] = idProp.mapsTo;
}
}

options.extra = association.props;
options.extra_info = {
table: association.mergeTable,
@@ -231,12 +241,22 @@ function extendInstance(Model, Instance, Driver, association, opts, createInstan

options.__merge = {
from : { table: association.mergeTable, field: Object.keys(association.mergeAssocId) },
to : { table: association.model.table, field: association.model.id },
to : { table: association.model.table, field: association.model.id.slice(0) }, // clone model id
where : [ association.mergeTable, {} ]
};
options.extra = association.props;
options.extra_info = {
table: association.mergeTable,

// Loop through the (cloned) association model id fields ... some of them may've been mapped to different
// names in the actual database - if so update to the mapped database column name
for(i=0; i<options.__merge.to.field.length; i++) {
var idProp = association.model.properties[options.__merge.to.field[i]];
if(idProp && idProp.mapsTo) {
options.__merge.to.field[i] = idProp.mapsTo;
}
}

options.extra = association.props;
options.extra_info = {
table: association.mergeTable,
id: util.values(Instance, Model.id),
id_prop: Object.keys(association.mergeId),
assoc_prop: Object.keys(association.mergeAssocId)
Loading

0 comments on commit 088cd9f

Please sign in to comment.