Entity Service API でコンポーネントとダイナミックゾーンを扱う
Caution
The Entity Service API is deprecated in Strapi v5. Please consider using the Document Service API instead.
Entity Service は コンポーネント と ダイナミックゾーン のロジックを扱う層です。エントリの作成・更新と同時に、コンポーネントとダイナミックゾーンを 作成 したり 更新 したりできます。
作成
コンポーネントは、Entity Service でエントリを作成するときに data に含めて作成できます。
strapi.entityService.create('api::article.article', {
data: {
myComponent: {
foo: 'bar',
},
},
});
ダイナミックゾーン(コンポーネントのリスト)も、エントリ作成時に data で渡します。
strapi.entityService.create('api::article.article', {
data: {
myDynamicZone: [
{
__component: 'compo.type',
foo: 'bar',
},
{
__component: 'compo.type2',
foo: 'bar',
},
],
},
});
更新
コンポーネントは、エントリ更新時に data で更新できます。コンポーネントに id が指定されていればそのレコードを更新し、指定がなければ古いものを削除して新規作成します。
strapi.entityService.update('api::article.article', 1, {
data: {
myComponent: {
id: 1, // id: 1 のコンポーネントを更新(未指定なら削除して新規作成)
foo: 'bar',
},
},
});
ダイナミックゾーンも同様です。id があれば更新、なければ新規追加し、配列に含まれない既存コンポーネントは削除されます。
strapi.entityService.update('api::article.article', 1, {
data: {
myDynamicZone: [
{
// 更新
id: 2,
__component: 'compo.type',
foo: 'bar',
},
{
// 新規追加(配列にない既存は削除)
__component: 'compo.type2',
foo: 'bar2',
},
],
},
});