I am using elementAPI to get data from a directory. The items listed in the directory have two categories assigned: Category level 1 country Category level 2 region
Below is how I am getting the data in the elementAPI plugin
'api/v2/country/<slug:{slug}>.json' => function($slug) {
$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->slug = $slug;
$category = $criteria->first();
return [
'elementType' => 'Entry',
'criteria' => [
'section' => 'directory',
'relatedTo' => ['targetElement' => $category],
'limit' => 1,
],
'transformer' => function(EntryModel $entry) {
return [
'country' => (string)$entry->country[0],
'port' => (string)$entry->country[1],
'title' => $entry->title,
'mapinfo' => $entry->mapaddress,
'url' => $entry->url
];
},
];
},
The addition of
'country' => (string)$entry->country[0],
'port' => (string)$entry->country[1],
adds 1.5 seconds to the request, compared to not putting them in which comes in at .656ms. It seems the category data is already requested outside of the transformer but i cant see how to get the category array into the transformer. I also can't see why the specifying the categories in the transformer is so 'expensive'.
Any ideas on speeding up would be very welcome.