ISSUE 825 - Losing OneToOne relation in the content manager - Fixes #825

This commit is contained in:
Adrien Lepoutre 2018-04-02 17:50:03 -04:00
parent 839b6069bb
commit 0b0ea75402

View File

@ -85,11 +85,11 @@ module.exports = {
} }
// Remove previous relationship asynchronously if it exists. // Remove previous relationship asynchronously if it exists.
virtualFields.push( let promise = new Promise(resolve => {
strapi.query(details.model || details.collection, details.plugin).findOne({ id : recordId }) strapi.query(details.model || details.collection, details.plugin).findOne({ id : recordId })
.then(record => { .then(record => {
if (record && _.isObject(record[details.via])) { if (record && _.isObject(record[details.via])) {
return module.exports.update.call(this, { module.exports.update.call(this, {
id: record[details.via][this.primaryKey] || record[details.via].id, id: record[details.via][this.primaryKey] || record[details.via].id,
values: { values: {
[current]: null [current]: null
@ -97,20 +97,34 @@ module.exports = {
parseRelationships: false parseRelationships: false
}); });
} }
resolve()
return Promise.resolve();
}) })
); })
virtualFields.push(promise);
// Update the record on the other side. // Updating the new relations
// When params.values[current] is null this means that we are removing the relation. // When params.values[current] is null this means that we are removing the relation.
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).update({ promise.then(response => {
id: recordId, // Recreate relation on the first side
values: { virtualFields.push(strapi.query(details.model || details.collection, details.plugin).update({
[details.via]: _.isNull(params.values[current]) ? null : value[this.primaryKey] || value.id || value._id id: recordId,
}, values: {
parseRelationships: false [details.via]: _.isNull(params.values[current]) ? null : value[this.primaryKey] || value.id || value._id
})); },
parseRelationships: false
}));
// Recreate relation on the other side if the value is not null
if (!_.isNull(params.values[current])) {
module.exports.update.call(this, {
id: value[this.primaryKey] || value.id || value._id,
values: {
[current]: recordId
},
parseRelationships: false
});
}
});
acc[current] = _.isNull(params.values[current]) ? null : value[current]; acc[current] = _.isNull(params.values[current]) ? null : value[current];
} }