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

Commit c77e298a by Jan Aagaard Meier Committed by Mick Hansen

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

1 parent 32b51c4a
Showing with 52 additions and 48 deletions
......@@ -428,66 +428,70 @@ BelongsToMany.prototype.injectAttributes = function() {
return this;
};
BelongsToMany.prototype.injectGetter = function(obj) {
var association = this;
BelongsToMany.prototype.get = function(instance, options) {
options = Utils.cloneDeep(options) || {};
obj[this.accessors.get] = function(options) {
options = Utils.cloneDeep(options) || {};
var association = this
, through = association.through
, scopeWhere
, throughWhere;
var instance = this
, through = association.through
, scopeWhere
, throughWhere;
if (association.scope) {
scopeWhere = _.clone(association.scope);
}
if (association.scope) {
scopeWhere = _.clone(association.scope);
}
options.where = {
$and: [
scopeWhere,
options.where
]
};
options.where = {
$and: [
scopeWhere,
options.where
]
};
if (Object(through.model) === through.model) {
throughWhere = {};
throughWhere[association.foreignKey] = instance.get(association.source.primaryKeyAttribute);
if (Object(through.model) === through.model) {
throughWhere = {};
throughWhere[association.foreignKey] = instance.get(association.source.primaryKeyAttribute);
if (through.scope) {
_.assign(throughWhere, through.scope);
}
if (through.scope) {
_.assign(throughWhere, through.scope);
}
//If a user pass a where on the options through options, make an "and" with the current throughWhere
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
if (options.through && options.through.where) {
throughWhere = {
$and: [throughWhere, options.through.where]
};
}
options.include = options.include || [];
options.include.push({
association: association.oneFromTarget,
attributes: options.joinTableAttributes,
required: true,
where: throughWhere
});
}
options.include = options.include || [];
options.include.push({
association: association.oneFromTarget,
attributes: options.joinTableAttributes,
required: true,
where: throughWhere
});
var model = association.target;
if (options.hasOwnProperty('scope')) {
if (!options.scope) {
model = model.unscoped();
} else {
model = model.scope(options.scope);
}
}
var model = association.target;
if (options.hasOwnProperty('scope')) {
if (!options.scope) {
model = model.unscoped();
} else {
model = model.scope(options.scope);
}
}
if (options.hasOwnProperty('schema')) {
model = model.schema(options.schema, options.schemaDelimiter);
}
if (options.hasOwnProperty('schema')) {
model = model.schema(options.schema, options.schemaDelimiter);
}
return model.findAll(options);
};
return model.findAll(options);
BelongsToMany.prototype.injectGetter = function(obj) {
var association = this;
obj[this.accessors.get] = function(options) {
return association.get(this, 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!