fix: load through collection before belongsToMany field bind (#1409)

This commit is contained in:
ChengLei Shao 2023-01-31 17:48:04 +08:00 committed by GitHub
parent 733e1ff60b
commit c239a5ad63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
import { Repository } from '@nocobase/database'; import { Repository } from '@nocobase/database';
import { CollectionModel } from '../models/collection'; import { CollectionModel } from '../models/collection';
import toposort from 'toposort'; import toposort from 'toposort';
import lodash from 'lodash';
interface LoadOptions { interface LoadOptions {
filter?: any; filter?: any;
@ -26,7 +27,8 @@ export class CollectionRepository extends Repository {
if (field['type'] === 'belongsToMany') { if (field['type'] === 'belongsToMany') {
const throughName = field.options.through; const throughName = field.options.through;
if (throughName) { if (throughName) {
throughModels.push(throughName); inheritedGraph.push([throughName, field.options.target]);
inheritedGraph.push([throughName, instance.get('name')]);
} }
} }
} }
@ -40,17 +42,10 @@ export class CollectionRepository extends Repository {
} }
} }
generalModels.sort((a, b) => { const sortedNames = [...toposort(inheritedGraph), ...lodash.difference(generalModels, throughModels)];
if (throughModels.includes(a)) {
return -1;
}
return 1; for (const instanceName of lodash.uniq(sortedNames)) {
}); if (!nameMap[instanceName]) continue;
const sortedNames = [...toposort(inheritedGraph), ...generalModels];
for (const instanceName of sortedNames) {
await nameMap[instanceName].load({ skipExist }); await nameMap[instanceName].load({ skipExist });
} }
} }