Search code examples
phprestzend-framework2laminas-api-tools

Apigility rest service - how to filter by a non unique column using db connected


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.


Solution

  • 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'
                ),
                ...