mirror of
https://github.com/strapi/strapi.git
synced 2025-11-10 15:19:00 +00:00
Fix one-to-one during first creation with Bookshelf
This commit is contained in:
parent
b08a4d811c
commit
1127ab1c85
@ -34,10 +34,6 @@ module.exports = {
|
|||||||
withRelated: this.associations.map(x => x.alias)
|
withRelated: this.associations.map(x => x.alias)
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("RECORD");
|
|
||||||
console.log(record ? record.toJSON() : record);
|
|
||||||
console.log("_______");
|
|
||||||
|
|
||||||
return record ? record.toJSON() : record;
|
return record ? record.toJSON() : record;
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -84,12 +80,11 @@ module.exports = {
|
|||||||
case 'oneToOne':
|
case 'oneToOne':
|
||||||
if (response[current] !== params.values[current]) {
|
if (response[current] !== params.values[current]) {
|
||||||
const value = _.isNull(params.values[current]) ? response[current] : params.values;
|
const value = _.isNull(params.values[current]) ? response[current] : params.values;
|
||||||
|
|
||||||
const recordId = _.isNull(params.values[current]) ? value[this.primaryKey] || value.id || value._id : value[current];
|
const recordId = _.isNull(params.values[current]) ? value[this.primaryKey] || value.id || value._id : value[current];
|
||||||
|
|
||||||
if (response[current] && _.isObject(response[current]) && response[current][this.primaryKey] !== value[current]) {
|
if (response[current] && _.isObject(response[current]) && response[current][this.primaryKey] !== value[current]) {
|
||||||
virtualFields.push(
|
virtualFields.push(
|
||||||
strapi.query(details.collection || details.model).update({
|
strapi.query(details.collection || details.model, details.plugin).update({
|
||||||
id: response[current][this.primaryKey],
|
id: response[current][this.primaryKey],
|
||||||
values: {
|
values: {
|
||||||
[details.via]: null
|
[details.via]: null
|
||||||
@ -101,9 +96,9 @@ module.exports = {
|
|||||||
|
|
||||||
// Remove previous relationship asynchronously if it exists.
|
// Remove previous relationship asynchronously if it exists.
|
||||||
virtualFields.push(
|
virtualFields.push(
|
||||||
strapi.query(details.model || details.collection).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]) && record[details.via][current] !== value[current]) {
|
||||||
return module.exports.update.call(this, {
|
return 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: {
|
||||||
@ -119,7 +114,7 @@ module.exports = {
|
|||||||
|
|
||||||
// Update the record on the other side.
|
// Update the record on the other side.
|
||||||
// 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).update({
|
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).update({
|
||||||
id: recordId,
|
id: recordId,
|
||||||
values: {
|
values: {
|
||||||
[details.via]: _.isNull(params.values[current]) ? null : value[this.primaryKey] || value.id || value._id
|
[details.via]: _.isNull(params.values[current]) ? null : value[this.primaryKey] || value.id || value._id
|
||||||
@ -151,7 +146,7 @@ module.exports = {
|
|||||||
toAdd.forEach(value => {
|
toAdd.forEach(value => {
|
||||||
value[details.via] = params.values[this.primaryKey] || params[this.primaryKey];
|
value[details.via] = params.values[this.primaryKey] || params[this.primaryKey];
|
||||||
|
|
||||||
virtualFields.push(strapi.query(details.model || details.collection).addRelation({
|
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).addRelation({
|
||||||
id: value[this.primaryKey] || value.id || value._id,
|
id: value[this.primaryKey] || value.id || value._id,
|
||||||
values: association.nature === 'manyToMany' ? params.values : value,
|
values: association.nature === 'manyToMany' ? params.values : value,
|
||||||
foreignKey: current
|
foreignKey: current
|
||||||
@ -161,7 +156,7 @@ module.exports = {
|
|||||||
toRemove.forEach(value => {
|
toRemove.forEach(value => {
|
||||||
value[details.via] = null;
|
value[details.via] = null;
|
||||||
|
|
||||||
virtualFields.push(strapi.query(details.model || details.collection).removeRelation({
|
virtualFields.push(strapi.query(details.model || details.collection, details.plugin).removeRelation({
|
||||||
id: value[this.primaryKey] || value.id || value._id,
|
id: value[this.primaryKey] || value.id || value._id,
|
||||||
values: association.nature === 'manyToMany' ? params.values : value,
|
values: association.nature === 'manyToMany' ? params.values : value,
|
||||||
foreignKey: current
|
foreignKey: current
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user