apostrophe-cms - 如何正确处理同名反向关系
问题描述
我有一种情况,有多个具有相同字段的部分。但是,当使用反向关系时,就会出现问题,因为它们似乎无法共存:
{
name: '_relatedPeople',
type: 'joinByArrayReverse',
withType: 'organization',
filters: {
projection: {
_url: 1,
title: 1,
tags: 1
}
},
},
{
name: '_relatedPeople',
type: 'joinByArrayReverse',
withType: 'event',
filters: {
projection: {
_url: 1,
title: 1,
tags: 1
}
},
},
事件类型覆盖组织类型。这是这两种类型都具有的字段(对于 DRY):
{
"label": "Related People",
"help": "",
"name": "_relatedPeople",
"type": "joinByArray",
"withType": ["person"],
"filters": {
"projection": {
"_url": 1,
"title": 1
}
}
}
我尝试使用idsField
and reverseOf
,但都失败了。人类型能够获得其他两种类型的反向关系的适当策略是什么?
解决方案
在筛选 ApostropheCMS 文档时,我通过反复试验找到了答案。我想我会在这里分享我的解决方案,以防其他人偶然发现相同类型的问题:
{
name: '_organizations', // type from which to get the pieces, prefixed with "_" and suffixed with "s"
reverseOf: '_relatedPeople', // field name from which to get the pieces
type: 'joinByArrayReverse',
withType: 'organization', // type which holds the pieces
filters: {
projection: {
_url: 1,
title: 1
}
},
},
{
name: '_events', // type from which to get the pieces, prefixed with "_" and suffixed with "s"
reverseOf: '_relatedPeople', // field name from which to get the pieces
type: 'joinByArrayReverse',
withType: 'event', // type which holds the pieces
filters: {
projection: {
_url: 1,
title: 1
}
},
},
请注意name
、reverseOf
和withType
属性,这是确保避免覆盖结果的方法。
然后可以使用data.piece._organizations
anddata.piece._events
并获取_relatedPeople
for each。
推荐阅读
- reactjs - 如何阅读 React.Children.map api 表示法?
- elasticsearch-6 - ERROR:root:Error running query: RequestError(400, u'parsing_exception', u'no [query] registered for [not]')
- c# - 在不需要并发时使用静态方法中的异步等待模式
- c# - 如何在 MonoBehaviour 中将可变列表公开为只读属性
- java - 字符串排序器返回退出代码 0 而不是字符串
- mysql - 如何在 MySQL 递归查询中求和
- python - 具有可调整大小列的 tkinter 列表框 - 递归致命错误
- slack - 如何为菜单组合斜杠命令、按钮和对话框?
- mysql - 如何将 10GB 旧表迁移到新表?
- ios - 从 web 的 txt 文件中导入数据