3

I'm trying to return just one entry from Element API based on custom fields created?

return [
    'endpoints' => [
        'students.json' => [
            'elementType' => 'Entry',
                'criteria' => [
                    'studentNumber' => craft()->request->getPost('studentNumber'),
                    'email' => craft()->request->getPost('email'),
                    'section' => 'students',
                ],
                'transformer' => function(EntryModel $entry) {
                return [
                    'studentNumber' => $entry->studentNumber,
                    'email' => $entry->email,
                ];
            }
        ]
    ]
];

The Fields are studentNumber and email but it seems to be returning everything. Even when specifying first => true

Anybody point me in the right direction on this? I'd imagine I'm missing something obvious?

Even hitting the url students.json?studentNumber=001 is returning everything.

1 Answers1

3

getPost() will search the request’s POST data, but you are setting the parameter values as query string params, so you should be using getQuery().

'studentNumber' => craft()->request->getQuery('studentNumber'),
'email' => craft()->request->getQuery('email'),
Brandon Kelly
  • 34,307
  • 2
  • 71
  • 137
  • Thanks, Brandon. Can't believe I missed that. Much appreciated. – andrew-caulfield Jul 26 '16 at 14:17
  • Can I ask one last thing? If I wanted to return a matrix in the response that is associated with the entry. Is this something that can be done? – andrew-caulfield Jul 26 '16 at 14:25
  • @Anderscc Yes. Other questions have touched on that which you can refer to as examples, e.g. http://craftcms.stackexchange.com/q/15995/9 (or feel free to post a new question if you're still having trouble with it) – Brandon Kelly Jul 26 '16 at 17:30