mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Fix m2o field value in nested o2ms
This commit is contained in:
@@ -102,7 +102,9 @@ export default class ItemsService implements AbstractService {
|
||||
return payload;
|
||||
});
|
||||
|
||||
await payloadService.processO2M(payloads);
|
||||
for (const key of primaryKeys) {
|
||||
await payloadService.processO2M(payloads, key);
|
||||
}
|
||||
|
||||
if (this.accountability) {
|
||||
const activityRecords = primaryKeys.map((key) => ({
|
||||
@@ -238,18 +240,23 @@ export default class ItemsService implements AbstractService {
|
||||
});
|
||||
|
||||
payload = await payloadService.processM2O(payload);
|
||||
payload = await payloadService.processValues('update', payload);
|
||||
|
||||
const payloadWithoutAliases = pick(
|
||||
let payloadWithoutAliases = pick(
|
||||
payload,
|
||||
columns.map(({ column }) => column)
|
||||
);
|
||||
|
||||
await trx(this.collection)
|
||||
.update(payloadWithoutAliases)
|
||||
.whereIn(primaryKeyField, keys);
|
||||
payloadWithoutAliases = await payloadService.processValues('update', payloadWithoutAliases);
|
||||
|
||||
await payloadService.processO2M(payload);
|
||||
if (Object.keys(payloadWithoutAliases).length > 0) {
|
||||
await trx(this.collection)
|
||||
.update(payloadWithoutAliases)
|
||||
.whereIn(primaryKeyField, keys);
|
||||
}
|
||||
|
||||
for (const key of keys) {
|
||||
await payloadService.processO2M(payload, key);
|
||||
}
|
||||
|
||||
if (this.accountability) {
|
||||
const activityRecords = keys.map((key) => ({
|
||||
|
||||
@@ -211,7 +211,7 @@ export default class PayloadService {
|
||||
/**
|
||||
* Recursively save/update all nested related o2m items
|
||||
*/
|
||||
async processO2M(payload: Partial<Item> | Partial<Item>[]) {
|
||||
async processO2M(payload: Partial<Item> | Partial<Item>[], parent?: PrimaryKey) {
|
||||
const relations = await this.knex
|
||||
.select<Relation[]>('*')
|
||||
.from('directus_relations')
|
||||
@@ -234,7 +234,7 @@ export default class PayloadService {
|
||||
const relatedRecords: Partial<Item>[] = payload[relation.one_field].map(
|
||||
(record: Partial<Item>) => ({
|
||||
...record,
|
||||
[relation.many_field]: payload[relation.one_primary],
|
||||
[relation.many_field]: parent || payload[relation.one_primary],
|
||||
})
|
||||
);
|
||||
|
||||
@@ -246,6 +246,7 @@ export default class PayloadService {
|
||||
const toBeCreated = relatedRecords.filter(
|
||||
(record) => record.hasOwnProperty(relation.many_primary) === false
|
||||
);
|
||||
|
||||
const toBeUpdated = relatedRecords.filter(
|
||||
(record) => record.hasOwnProperty(relation.many_primary) === true
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user