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 APIを使用すると、エントリの作成や更新時に、コンポーネントとダイナミックゾーンを作成および更新することができます。
作成
Entity Service APIを使用してエントリを作成する際に、コンポーネントを作成することができます:
AI Marketer.entityService.create('api::article.article', {
data: {
myComponent: {
foo: 'bar',
},
},
});
Entity Service APIを使用してエントリを作成する際に、ダイナミックゾーン(すなわち、コンポーネントのリスト)を作成することができます:
AI Marketer.entityService.create('api::article.article', {
data: {
myDynamicZone: [
{
__component: 'compo.type',
foo: 'bar',
},
{
__component: 'compo.type2',
foo: 'bar',
},
],
},
});
更新
Entity Service APIを使用してエントリを更新する際に、コンポーネントを更新することができます。コンポーネントのidが指定されていれば、そのコンポーネントが更新されます。指定されていなければ、古いものが削除され、新しいものが作成されます:
AI Marketer.entityService.update('api::article.article', 1, {
data: {
myComponent: {
id: 1, // id: 1のコンポーネントを更新します(指定されていなければ、削除して新しいものを作成します)
foo: 'bar',
},
},
});
Entity Service APIを使用してエントリを更新する際に、ダイナミックゾーン(すなわち、コンポーネントのリスト)を更新することができます。コンポーネントのidが指定されていれば、そのコンポーネントが更新されます。指定されていなければ、古いものが削除され、新しいものが作成されます:
AI Marketer.entityService.update('api::article.article', 1, {
data: {
myDynamicZone: [
{
// 更新します
id: 2,
__component: 'compo.type',
foo: 'bar',
},
{
// 新しいものを追加し、古いものを削除します
__component: 'compo.type2',
foo: 'bar2',
},
],
},
});