1

I'm producing a list of entries that each use an image asset from a related entry. I see in this ticket how to get an asset's data, and I've combined that with how I learned to output an object's data from a Mijingo video (via the array_map function).

This approach works, but seeing how there are about 130 entries in the section, the load time is a whopping 2.31s versus when I take out the related entry's asset code, which then results in a much smaller 500ms load time. Is there a better way to access this data?

Here's the code I'm using:

return [
    'endpoints' => [
        'api/pressEntries.json' => [
            'elementType' => 'Entry',
            'criteria' => ['section' => 'pressEntries'],
            'transformer' => function(EntryModel $entry) {
                return [
                    'id'                => $entry->id,
                    'title'             => $entry->title,
                    'slug'              => $entry->slug,
                    'postDate'          => $entry->postDate,
                    'type'              => $entry->type->name,
                    'url'               => $entry->url,
                    'jsonUrl'           => UrlHelper::getUrl("api/press/{$entry->id}.json"),
                    'relatedRecording'  => array_map(function(EntryModel $album) {
                        $data = [
                            'id'    => $album->id,
                            'title' => $album->title,
                        ];

                        $artwork = $album->recordingArtwork->first();
                        if ($artwork) {
                            $data['artwork'] = $artwork->url;
                        }
                        return $data;

                    }, $entry->relatedRecording->find()),
                ];
            },
        ]
    ]
];
1

Sounds like you need to do some eager loading of your assets.

Here are some examples of how to do that in the Element API plugin:

ElementAPI handling CategoryModel and returning a heirachical object

Getting categories in elementAPI performance issues and help required

1
  • Those articles were helpful, thanks Brad! I think I got it working now, but I ended up removing the $artwork bc I didn't need it in the end. And for some reason I had to remove the ->find(). Load time decreased by 2/3! – Ryan Mar 8 '17 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.