fix: 上游跟踪,fix- getSourceKeyByAssocation #3947
This commit is contained in:
parent
8c71fbd5a2
commit
9f12aa8ae5
@ -131,6 +131,29 @@ export class CollectionManager {
|
|||||||
return this.getCollection(collectionName)?.getFields(predicate) || [];
|
return this.getCollection(collectionName)?.getFields(predicate) || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSourceKeyByAssocation(associationName: string) {
|
||||||
|
if (!associationName) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const field = this.getCollectionField(associationName);
|
||||||
|
// 字段不存在,返回空
|
||||||
|
if (!field) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// hasOne 和 hasMany 和 belongsToMany 的字段存在 sourceKey,所以会直接返回 sourceKey;
|
||||||
|
if (field.sourceKey) {
|
||||||
|
return field.sourceKey;
|
||||||
|
}
|
||||||
|
// belongsTo 不存在 sourceKey,所以会使用 filterTargetKey;
|
||||||
|
const sourceCollection = this.getCollection(associationName.split('.')[0]);
|
||||||
|
// source collection 不存在,返回空
|
||||||
|
if (!sourceCollection) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 后面的主键和 id 是为了保险起见加上的;
|
||||||
|
return sourceCollection.filterTargetKey || sourceCollection.getPrimaryKey() || 'id';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
@ -23,13 +23,11 @@ export const DataBlockResourceProvider: FC<{ children?: ReactNode }> = ({ childr
|
|||||||
return sourceId;
|
return sourceId;
|
||||||
}
|
}
|
||||||
if (association && parentRecord) {
|
if (association && parentRecord) {
|
||||||
const associationCollection = cm.getCollection(association);
|
const sourceKey = cm.getSourceKeyByAssocation(association);
|
||||||
if (associationCollection) {
|
|
||||||
const parentRecordData = parentRecord instanceof CollectionRecord ? parentRecord.data : parentRecord;
|
const parentRecordData = parentRecord instanceof CollectionRecord ? parentRecord.data : parentRecord;
|
||||||
return parentRecordData[associationCollection.sourceKey || 'id'];
|
return parentRecordData[sourceKey];
|
||||||
}
|
}
|
||||||
}
|
}, [association, sourceId, parentRecord]);
|
||||||
}, [sourceId, parentRecord]);
|
|
||||||
|
|
||||||
const resource = useMemo(() => {
|
const resource = useMemo(() => {
|
||||||
if (association) {
|
if (association) {
|
||||||
|
@ -8,19 +8,5 @@ import { useCollectionManager } from '../../data-source/collection/CollectionMan
|
|||||||
*/
|
*/
|
||||||
export const useSourceKey = (association: string) => {
|
export const useSourceKey = (association: string) => {
|
||||||
const cm = useCollectionManager();
|
const cm = useCollectionManager();
|
||||||
|
return cm.getSourceKeyByAssocation(association);
|
||||||
if (!association) return;
|
|
||||||
|
|
||||||
const associationField = cm.getCollectionField(association);
|
|
||||||
|
|
||||||
if (!associationField) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const sourceCollection = cm.getCollection<InheritanceCollectionMixin>(association.split('.')[0]);
|
|
||||||
|
|
||||||
// 1. hasOne 和 hasMany 和 belongsToMany 的字段存在 sourceKey,所以会直接返回 sourceKey;
|
|
||||||
// 2. belongsTo 不存在 sourceKey,所以会使用 filterTargetKey;
|
|
||||||
// 3. 后面的主键和 id 是为了保险起见加上的;
|
|
||||||
return associationField.sourceKey || sourceCollection.filterTargetKey || sourceCollection.getPrimaryKey() || 'id';
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user