REST API - how to query

Hello friends, I have successfully created my first REST API and I can list out all persons and get a specific person like so :

I have followed the official documentation and my controller is extending the modRestController, and I have checked the class on github.
So to my question: what is the syntax for querying on a specific field?
For example to fetch all persons with firstname «Roger». I would expect something like this:

I see that the modRestController has a method called addSearchQuery(xPDOQuery $c)

From my understanding this should be out of the box ready, but I cannot for the life of me figure out what the url should look like.

Any and all help is appreciated.

I believe by default the parameter is called search.

Also define the search fields in this property

1 Like

Thank you for your quick reply, I’m not at my computer now but will try that later. But assumning I want to query all persons with firstname «Roger» and say city=Oslo, and perhaps order the result on another field again… is that possible, or will it be necessary to write some custom code? The documenation IMHO is quite sparse on this topic, and I have spent a considerable amount of time scouring the forums. :slight_smile:

Edit: ah I think I understand now. If I understand it correctly I can add as many query-params as I need using the same pattern as for addSearchQuery(), and then just write my own xPDO queries.
Correct? :slight_smile:

For the ordering you should be able to use ?sort=field&dir=desc out of the box.

From what I can tell, when you want to search for two different search values, then you have to override the function addSearchQuery in your own class.

1 Like

I see in modRestController.addSearchQuery() a call to a variable searchFields… how do I populated this?

Thanks again, much appreciated. I also found this:

     * Allows manipulation of the query object before the COUNT statement is called on listing calls. Override to
     * provide custom functionality.
     * @param xPDOQuery $c
     * @return xPDOQuery
    protected function prepareListQueryBeforeCount(xPDOQuery $c) {
        return $c;

Probably here I can populate searchFields… think I will just have do some testing. Regarding documenation: I would be happy to contribute improving it regarding this specific topic.

Your class extends modRestController. Just set the searchFields as a property:

class MyControllerPerson extends modRestController {

	public $searchFields = array('firstname','surname');


But when you override the addSearchQuery() function you can also just hardcode the values.

In the function addSearchQuery() just use e.g. $this->getProperty('city') to read the value from a parameter named city ?city=Oslo.

1 Like

Excatly so, now I understand. Thank you so much for your help @halftrainedharry, you have saved me a lot of time! :grinning: