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

1 Answer 1

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],
    ];
},
4
  • yeah, I'd thought I could have done that but nothing comes from it. Commented Dec 20, 2015 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
    Commented Dec 20, 2015 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 Commented Dec 20, 2015 at 19:35
  • Weird... CategoryModel's __toString() magic method should be getting called there. Regardless, I'll update my answer.
    – Brad Bell
    Commented Dec 20, 2015 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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