2

I have a db-connected rest service, and I only managed to so far get the full collection from the database or a single entity by its ID.

I can't find a proper guide to explain how to use the GET url parameters to filter by other fields, and how do I choose for example whether its "LIKE" or LIKE %%" or other operators for that matter.

Dalai Lama
  • 404
  • 4
  • 20

1 Answers1

3

This is my experience with CodeConnected Services. YMMV..

Retrieving URL Parameters - Controller/Resource class.

Your controller needs to retrieve them from the $this->getEvent()

 /**
 * Fetch a single Entity by ID, with some Query Params
 */
public function fetch($entity_id)
{
    // retrieve the query parameters\
    $queryParams = $this->getEvent()->getQueryParams();
}

Secondly , only parameters approved on your module.config.php will make it past the validator/filter part of Apigility. Notice Collection Query Whitelist

module.config.php inside your service's module folder

'ServiceName\\V1\\Rest\\ServiceName\\Controller' => array(
            ...
            'entity_http_methods' => array(
                0 => 'GET',
                1 => 'PATCH',
                2 => 'PUT',
                3 => 'DELETE',
            ),
            'collection_http_methods' => array(
                0 => 'GET',
                1 => 'POST',
            ),
            'collection_query_whitelist' => array(
                0 => 'username',
                1 => 'entity_provider',
                2 => 'entity_type',
                3 => 'entity_date_range',
                4 => 'sort_by',
                5 => 'sort_order'
            ),
            ...
Erik
  • 2,782
  • 3
  • 34
  • 64
  • Testing it, let me get back to you on that – Dalai Lama Oct 29 '14 at 09:48
  • Oh yes, I actually found out that Apigility editor lets you whitelist these parameters through its editor. Once I whitelist a param it does exactly what you wrote over here. Then again I wanted to do it in a db-connected and not in a code connected... and even though I did move to code connected it still does not really answer the question does it? Ill mark it anyway. – Dalai Lama Nov 23 '14 at 09:21
  • sorry I can't help with DB-Connected, I rarely (never, except in a tutorial) use them. – Erik Nov 25 '14 at 19:47
  • Don't be! You still helped me in a way to aim me towards what I need. – Dalai Lama Nov 26 '14 at 08:06