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

提醒

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