# 使用实体服务 API 填充

> Source: https://strapi.nodejs.cn/cms/api/entity-service/populate

🌐 Populating with the Entity Service API

实体服务 API 的 `populate` 参数用于获取关系、组件和动态区域。使用 `populate: '*'` 获取所有根级关系，使用数组获取特定字段，使用对象进行带过滤器和嵌套填充的高级查询，或使用片段处理多态内容结构。

🌐 The Entity Service API's `populate` parameter retrieves relations, components, and dynamic zones. Use `populate: '*'` for all root-level relations, arrays for specific fields, objects for advanced queries with filters and nested populating, or fragments for polymorphic content structures.

:::caution

在 Strapi v5 中，实体服务 API 已被弃用。请考虑改用 [文档服务 API](/cms/api/document-service)。

🌐 The Entity Service API is deprecated in Strapi v5. Please consider using the [Document Service API](/cms/api/document-service) instead.

:::

[实体服务 API](/cms/api/entity-service) 默认不会填充关系、组件或动态区域，这意味着不使用 `populate` 参数的实体服务 API 查询不会返回关于关系、组件或动态区域的信息。

🌐 The [Entity Service API](/cms/api/entity-service) does not populate relations, components or dynamic zones by default, which means an Entity Service API query that does not use the `populate` parameter will not return information about relations, components, or dynamic zones.

## 基本填充 {#basic-populating}

🌐 Basic populating

要填充所有根级关系，请使用 `populate: '*'`：

🌐 To populate all the root level relations, use `populate: '*'`:

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  populate: '*',
});
```

通过传递属性名称数组来填充各种组件或关系字段：

🌐 Populate various component or relation fields by passing an array of attribute names:

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  populate: ['componentA', 'relationA'],
});
```

## 高级填充 {#advanced-populating}

🌐 Advanced populating

可以传递一个对象以进行更高级的填充：

🌐 An object can be passed for more advanced populating:

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  populate: {
    relationA: true,
    repeatableComponent: {
      fields: ['fieldA'],
      filters: {},
      sort: 'fieldA:asc',
      populate: {
        relationB: true,
      },
    },
  },
});
```

可以通过使用[`filters`参数](/cms/api/entity-service/filter)并选择或填充嵌套关系或组件来实现复杂填充：

🌐 Complex populating can be achieved by using the [`filters` parameter](/cms/api/entity-service/filter) and select or populate nested relations or components:

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  populate: {
    relationA: {
      filters: {
        name: {
          $contains: 'Strapi',
        },
      },
    },

    repeatableComponent: {
      fields: ['someAttributeName'],
      sort: ['someAttributeName'],
      populate: {
        componentRelationA: true,
      },
    },
  },
});
```

## 填充碎片 {#populate-fragments}

🌐 Populate fragments

在处理多态内容结构（动态区域、多态关系等）时，可以使用填充片段来更好地控制填充策略。

🌐 When dealing with polymorphic content structures (dynamic zones, polymorphic relations, etc...), it is possible to use populate fragments to have a better granularity on the populate strategy.

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  populate: {
    dynamicZone: {
      on: {
        'components.foo': {
          fields: ['title'],
          filters: { title: { $contains: 'strapi' } },
        },
        'components.bar': {
          fields: ['name'],
        },
      },
    },

    morphAuthor: {
      on: {
        'plugin::users-permissions.user': {
          fields: ['username'],
        },
        'api::author.author': {
          fields: ['name'],
        },
      },
    },
  },
});
```
