跳到主要内容

Creating components and dynamic zones with the Entity Service API

Caution

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

The Entity Service is the layer that handles components and dynamic zones logic. With the Entity Service API, components and dynamic zones can be created and updated while creating or updating entries.

Creation

A component can be created while creating an entry with the Entity Service API:

strapi.entityService.create('api::article.article', {
data: {
myComponent: {
foo: 'bar',
},
},
});

A dynamic zone (i.e. a list of components) can be created while creating an entry with the Entity Service API:

strapi.entityService.create('api::article.article', {
data: {
myDynamicZone: [
{
__component: 'compo.type',
foo: 'bar',
},
{
__component: 'compo.type2',
foo: 'bar',
},
],
},
});

Update

A component can be updated while updating an entry with the Entity Service API. If a component id is specified, the component is updated, otherwise the old one is deleted and a new one is created:

strapi.entityService.update('api::article.article', 1, {
data: {
myComponent: {
id: 1, // will update component with id: 1 (if not specified, would have deleted it and created a new one)
foo: 'bar',
},
},
});

A dynamic zone (i.e. a list of components) can be updated while updating an entry with the Entity Service API. If a component id is specified, the component is updated, otherwise the old one is deleted and a new one is created:

strapi.entityService.update('api::article.article', 1, {
data: {
myDynamicZone: [
{
// will update
id: 2,
__component: 'compo.type',
foo: 'bar',
},
{
// will add a new & delete old ones
__component: 'compo.type2',
foo: 'bar2',
},
],
},
});