Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong createInstance in hasMany associations. MongoDB. #479

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/Drivers/DML/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,24 @@ Driver.prototype.hasMany = function (Model, association) {
}

conditions._id = { $in: [] };
options.extra = {};
var extra = {};
for (var i = 0; i < docs[0][association.name].length; i++) {
conditions._id.$in.push(new mongodb.ObjectID(docs[0][association.name][i]._id));
options.extra[docs[0][association.name][i]._id] = docs[0][association.name][i];
extra[docs[0][association.name][i]._id] = docs[0][association.name][i];
}

if (options.order) {
options.order[0] = options.order[0][1];
options.order = Utilities.standardizeOrder(options.order);
}

options.extra_props = association.props;
options.createInstance = createInstance;

return driver.find(null, association.model.table, conditions, options, cb);
return association.model.find(conditions,options,function(e,docs){
var i, len;
for(i = 0, len = docs.length; i < len; i++){
if (extra.hasOwnProperty(docs[i][association.model.id]))
docs[i].extra = extra[docs[i][association.model.id]];
}
cb(e,docs);
});
});
},
add: function (Instance, Association, data, cb) {
Expand Down
10 changes: 10 additions & 0 deletions test/integration/association-hasmany.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe("hasMany", function () {

should(Array.isArray(pets));
pets.length.should.equal(2);
pets[0].model().should.equal(Pet);
pets[0].name.should.equal("Mutt");
pets[1].name.should.equal("Deco");

Expand All @@ -87,6 +88,15 @@ describe("hasMany", function () {
});
});

it ("should return proper instance model", function(done){
Person.find({ name: "John" }, function (err, people) {
people[0].getPets("-name", function (err, pets) {
pets[0].model().should.equal(Pet);
return done();
});
});
});

it("should allow to specify order as Array", function (done) {
Person.find({ name: "John" }, function (err, people) {
should.equal(err, null);
Expand Down
9 changes: 9 additions & 0 deletions test/integration/association-hasone.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ describe("hasOne", function() {
});
});

it("should return proper instance model", function (done) {
Leaf.one({ size: 14 }, function (err, leaf) {
leaf.getTree(function (err, tree) {
tree.model().should.equal(Tree);
return done();
});
});
});

it("get should get the association with a shell model", function (done) {
Leaf(leafId).getTree(function (err, tree) {
should.not.exist(err);
Expand Down