Skip to main content
Unlisted page
This page is unlisted. Search engines will not index it, and only users having a direct link can access it.

使用实体服务 API 填充

🌐 Populating with the Entity Service API

Caution

在 Strapi v5 中,实体服务 API 已被弃用。请考虑改用 文档服务 API

🌐 The Entity Service API is deprecated in Strapi v5. Please consider using the Document Service API instead.

实体服务 API 默认不会填充关系、组件或动态区域,这意味着不使用 populate 参数的实体服务 API 查询不会返回关于关系、组件或动态区域的信息。

🌐 The Entity Service API 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

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

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

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

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

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

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

高级填充

🌐 Advanced populating

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

🌐 An object can be passed for more advanced populating:

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

可以通过使用filters参数并选择或填充嵌套关系或组件来实现复杂填充:

🌐 Complex populating can be achieved by using the filters parameter and select or populate nested relations or components:

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

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

🌐 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.

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'],
},
},
},
},
});