【问题标题】:How to get related object of existing relation如何获取现有关系的相关对象
【发布时间】:2020-12-18 01:06:30
【问题描述】:

我有两个模块,BlogAuthors。对于每篇博客文章,我想从 Authors 模块中选择作者并显示他的姓名和照片:

blog/index.js:

module.exports = {
    extend: 'apostrophe-pieces',
    name: 'blog',
    label: 'Article',
    pluralLabel: 'Articles',
    contextual: true,
    sort: { date: -1 },
    arrangeFields: [
        {
            name: 'content',
            label: 'Content',
            fields: [ 'title', 'shortTitle', 'slug', 'published', 'tags', 'date', 'perex', 'shortPerex', '_author', '_thumbnail' ]
        }
    ],
    addColumns: [
        {
            name: 'date',
            label: 'Date',
            sort: {
                date: -1
            }
        },
        {
            name: 'slug',
            label: 'Slug',
            sort: {
                slug: 1
            }
        }
    ],
    addFields: [
        {
            name: 'title',
            label: 'Title',
            type: 'string',
            required: true
        },
        {
            name: 'shortTitle',
            label: 'Short title',
            type: 'string',
            required: false
        },
        {
            name: 'shortPerex',
            label: 'Short perex',
            type: 'string',
            textarea: true,
            required: false
        },
        {
            name: 'perex',
            label: 'Long perex',
            type: 'string',
            textarea: true,
            required: false
        },
        {
            name: 'date',
            label: 'Date',
            type: 'date',
            pikadayOptions: {
                firstDay: 1
            },
            required: true,
            sortify: true
        },
        {
            name: '_author',
            type: 'joinByOne',
            withType: 'author',
            label: 'Author',
            idField: 'authorId',
        },
        {
            name: '_thumbnail',
            type: 'joinByOne',
            withType: 'apostrophe-image',
            label: 'Thumbnail',
            required: true,
        }
    ]
};

authors/index.js

module.exports = {
    extend: 'apostrophe-pieces',
    name: 'author',
    label: 'Author',
    pluralLabel: 'Authors',
    searchable: false,
    arrangeFields: [
        {
            name: 'basics',
            label: 'Basics',
            fields: [ 'title', 'nameCZ', 'nameEN', '_thumbnail', 'slug', 'published', 'tags' ]
        }
    ],
    addFields: [
        {
            name: 'name',
            type: 'string',
            label: 'Name',
            required: true
        },
        {
            name: '_thumbnail',
            type: 'joinByOne',
            idField: 'thumbnailId',
            withType: 'apostrophe-image',
            label: 'Picture',
            required: true,
        }
    ]
}

blog-pages/index.js:

module.exports = {
    extend: 'apostrophe-pieces-pages',
    perPage: 99,
    sort: { date: -1 },
}

blog-pages/views/show.html:

{% extends 'layout.html' %}

{% block title %}{{ data.piece.title }} | Costlocker{% endblock %}

{% block main %}
    {% set item = data.piece %}
    <h5>Author:</h5>
    <pre>{{ item._author | json}}</pre>
    <hr>
    <h5>Thumbnail:</h5>
    <pre>{{ item._author._thumbnail | json}}</pre>
{% endblock %}

结果:

<h5>Author:</h5>
{
    "_id": "ckiolmxue003kenrrqjdj20vz",
    "published": true,
    "trash": false,
    "siteMapPriority": null,
    "type": "author",
    "title": "David Maralík",
    "name": "David Maralik",
    "thumbnailId": "ckiolmjqb0033enrr4ol0wl0u",
    "slug": "david-maralík",
    "tags": [

    ],
    "openGraphTitle": "",
    "openGraphDescription": "",
    "openGraphType": "",
    "openGraphImage": {
        "items": [

        ],
        "type": "area",
        "_edit": true,
        "_docId": "ckiolnuto0058enrr99bl4d4j",
        "_dotPath": "_author.openGraphImage"
    },
    "workflowLocale": "en-draft",
    "workflowGuid": "ckiolmxue003jenrric96iw8g",
    "createdAt": "2020-12-14T13:34:54.422Z",
    "titleSortified": "david maralík",
    "updatedAt": "2020-12-14T13:34:54.424Z",
    "highSearchText": "david maralík david maralík david maralík david maralík david maralik david maralík",
    "highSearchWords": [
        "david",
        "maralík",
        "maralik"
    ],
    "lowSearchText": "david maralík david maralík david maralík david maralík david maralik david maralík",
    "searchSummary": "",
    "viewGroupsIds": [

    ],
    "viewUsersIds": [

    ],
    "editGroupsIds": [

    ],
    "editUsersIds": [

    ],
    "docPermissions": [

    ],
    "editGroupsRelationships": {
    },
    "editUsersRelationships": {
    },
    "loginRequired": null,
    "viewGroupsRelationships": {
    },
    "viewUsersRelationships": {
    },
    "workflowLastEditor": "admin",
    "workflowLastEditorId": "ckioleo9j00068qrr59o3k4r4",
    "workflowModified": false,
    "workflowLastCommitted": {
        "at": "2020-12-14T13:41:52.420Z",
        "user": {
            "username": "admin",
            "title": "admin",
            "_id": "ckioleo9j00068qrr59o3k4r4"
        }
    },
    "_edit": true,
    "_publish": true,
    "_originalWidgets": {
    }
}
<h5>Thumbnail:</h5>
null

如何获得_thumbnail.attachmentAuthor

谢谢

【问题讨论】:

    标签: apostrophe-cms


    【解决方案1】:

    我有几点意见:

    1. 像这样定义缩略图字段:
    {
          name: 'image',
          label: 'Image',
          type: 'singleton',
          widgetType: 'apostrophe-images',
          options: {
            limit: 1,
            focalPoint: true
          }
        }
    

    您添加它的方式现在看起来更像是从另一种作品类型添加一个实例,而不是这个作者的字段。

    1. 当您将作者添加到博客文章时,请执行以下操作:
    {
      name: '_author',
      label: 'Author',
      type: 'joinByOne',
      withType: 'author',
      required: true,
      filters: {
        projection: {
          title: 1,
          thumbnail: 1
        }
      }
    }
    

    投影可帮助您仅获得所需的字段,而不是其他所有字段。

    我相信这应该可以帮助您获得这样的缩略图:

    {{ item._author.thumbnail | json}}
    

    【讨论】:

      猜你喜欢
      • 2018-04-04
      • 2015-04-12
      • 2021-07-10
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多