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

Commit acce21d4 by Jan Aagaard Meier Committed by Mick Hansen

fix(btm): Expose belongs to many getter similar to other associations (#6552)

1 parent 2ae4d9a8
Showing with 52 additions and 47 deletions
...@@ -421,65 +421,70 @@ class BelongsToMany extends Association { ...@@ -421,65 +421,70 @@ class BelongsToMany extends Association {
return this; return this;
} }
injectGetter(obj) { get(instance, options) {
const association = this; options = Utils.cloneDeep(options) || {};
obj[this.accessors.get] = function(options) { const association = this;
options = Utils.cloneDeep(options) || {}; const through = association.through;
let scopeWhere;
let throughWhere;
const through = association.through; if (association.scope) {
let scopeWhere; scopeWhere = _.clone(association.scope);
let throughWhere; }
if (association.scope) { options.where = {
scopeWhere = _.clone(association.scope); $and: [
} scopeWhere,
options.where
]
};
options.where = { if (Object(through.model) === through.model) {
$and: [ throughWhere = {};
scopeWhere, throughWhere[association.foreignKey] = instance.get(association.source.primaryKeyAttribute);
options.where
]
};
if (Object(through.model) === through.model) { if (through.scope) {
throughWhere = {}; _.assign(throughWhere, through.scope);
throughWhere[association.foreignKey] = this.get(association.source.primaryKeyAttribute); }
if (through.scope) { //If a user pass a where on the options through options, make an "and" with the current throughWhere
_.assign(throughWhere, through.scope); if (options.through && options.through.where) {
} throughWhere = {
$and: [throughWhere, options.through.where]
};
}
//If a user pass a where on the options through options, make an "and" with the current throughWhere options.include = options.include || [];
if (options.through && options.through.where) { options.include.push({
throughWhere = { association: association.oneFromTarget,
$and: [throughWhere, options.through.where] attributes: options.joinTableAttributes,
}; required: true,
} where: throughWhere
});
}
options.include = options.include || []; let model = association.target;
options.include.push({ if (options.hasOwnProperty('scope')) {
association: association.oneFromTarget, if (!options.scope) {
attributes: options.joinTableAttributes, model = model.unscoped();
required: true, } else {
where: throughWhere model = model.scope(options.scope);
});
} }
}
let model = association.target; if (options.hasOwnProperty('schema')) {
if (options.hasOwnProperty('scope')) { model = model.schema(options.schema, options.schemaDelimiter);
if (!options.scope) { }
model = model.unscoped();
} else {
model = model.scope(options.scope);
}
}
if (options.hasOwnProperty('schema')) { return model.findAll(options);
model = model.schema(options.schema, options.schemaDelimiter); }
}
return model.findAll(options); injectGetter(obj) {
const association = this;
obj[this.accessors.get] = function(options) {
return association.get(this, options);
}; };
obj[this.accessors.count] = function(options) { obj[this.accessors.count] = function(options) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!