0

The category count is only ever 1 and I need to output the name of the category the entry belongs to.

Would there need to be a cross-over between the EntryModel and CategoryModel?

'transformer' => function(EntryModel $entry) {
                return [
                    'title' => $entry->title,
                    'url' => $entry->url,
                    'jsonUrl' => UrlHelper::getUrl("jobs/{$entry->id}.json"),
                    'postDate' => $entry->postDate->getTimestamp() * 1000,
                    'id' => $entry->id,
                    'jobOverview' => (string) $entry->jobOverview,
                    'category' => $entry->jobCategory[0],
                ];
            },
3

Same as you would access it from a Twig template. If you're positive there will always be one category selected, then this would work:

'transformer' => function(EntryModel $entry) {
    return [
        'category' => (string)$entry->categoryHandle[0],
    ];
},
  • yeah, I'd thought I could have done that but nothing comes from it. – Steven Grant Dec 20 '15 at 16:12
  • Are you sure you have the correct $entry? What do you get if you dump $entry->categoryHandle[0] from a normal Twig template? – Brad Bell Dec 20 '15 at 18:52
  • Should have clarified, this is for all jobs, not a single entry endpoint. I found that 'category' => (string) $entry->jobCategory[0], works though – Steven Grant Dec 20 '15 at 19:35
  • Weird... CategoryModel's __toString() magic method should be getting called there. Regardless, I'll update my answer. – Brad Bell Dec 20 '15 at 21:53

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.