Merge pull request #3158 from strapi/fix/issue-3156-one-to-many-deleted

Fix one to many update
This commit is contained in:
Jim LAURIE 2019-04-18 17:24:33 +02:00 committed by GitHub
commit b3ca89b405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 8 deletions

View File

@ -121,16 +121,16 @@ module.exports = {
// set relation to null for all the ids not in the list
const currentIds = response[current];
const diff = _.differenceWith(property, currentIds, (a, b) => {
const toRemove = _.differenceWith(currentIds, property, (a, b) => {
return `${a[assocModel.primaryKey] || a}` === `${b[assocModel.primaryKey] || b}`;
});
const updatePromise = assocModel
.where(assocModel.primaryKey, 'in', currentIds.map(val => val[assocModel.primaryKey]||val))
.where(assocModel.primaryKey, 'in', toRemove.map(val => val[assocModel.primaryKey]||val))
.save({ [details.via] : null }, { method: 'update', patch: true, require: false })
.then(() => {
return assocModel
.where(assocModel.primaryKey, 'in', diff.map(val => val[assocModel.primaryKey]||val))
.where(assocModel.primaryKey, 'in', property.map(val => val[assocModel.primaryKey]||val))
.save({ [details.via] : primaryKeyValue }, { method: 'update', patch: true, require: false });
});

View File

@ -67,23 +67,21 @@ module.exports = {
return _.set(acc, current, property);
}
case 'oneToMany': {
// receive array of ids or array of objects with ids
// set relation to null for all the ids not in the list
const currentIds = response[current];
const diff = _.differenceWith(property, currentIds, (a, b) => {
const toRemove = _.differenceWith(currentIds, property, (a, b) => {
return `${a[assocModel.primaryKey] || a}` === `${b[assocModel.primaryKey] || b}`;
});
const updatePromise = assocModel.updateMany({
[assocModel.primaryKey]: {
$in: currentIds.map(val => new mongoose.Types.ObjectId(val[assocModel.primaryKey]||val))
$in: toRemove.map(val => new mongoose.Types.ObjectId(val[assocModel.primaryKey]||val))
}
}, { [details.via] : null })
.then(() => {
return assocModel.updateMany({
[assocModel.primaryKey]: {
$in: diff.map(val => new mongoose.Types.ObjectId(val[assocModel.primaryKey]||val))
$in: property.map(val => new mongoose.Types.ObjectId(val[assocModel.primaryKey]||val))
}
}, { [details.via] : primaryKeyValue });
});