メインコンテンツまでスキップ
非公開のページ
このページは非公開です。 検索対象外となり、このページのリンクに直接アクセスできるユーザーのみに公開されます。

Entity Service API でのポピュレート

Caution

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

Entity Service API は既定ではリレーション、コンポーネント、ダイナミックゾーンをポピュレートしません。populate を付けないクエリでは、これらの情報は返りません。

基本的なポピュレート

ルート階層のリレーションをすべてポピュレートするには populate: '*' を使います。

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

コンポーネントやリレーションのフィールド名を配列で渡して個別に指定します。

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

高度なポピュレート

オブジェクトを渡すと、より細かく指定できます。

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

filters パラメータ と組み合わせて、ネストしたリレーションやコンポーネントを絞り込みながらポピュレートできます。

const entries = await strapi.entityService.findMany('api::article.article', {
populate: {
relationA: {
filters: {
name: {
$contains: 'Strapi',
},
},
},

repeatableComponent: {
fields: ['someAttributeName'],
sort: ['someAttributeName'],
populate: {
componentRelationA: true,
},
},
},
});

ポピュレートのフラグメント

多形構造(ダイナミックゾーン、多形リレーションなど)では、on を使ったポピュレートフラグメントでコンポーネント種別ごとに戦略を分けられます。

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