Skip to content

Apply filters in custom data provider #262

@berislavsapina

Description

@berislavsapina

Hi,
i have a problem.

I can not figure it out how to apply filters for custom collection data provider. In this examples provider does not do anything but filters are problem.

This is my resource file:

resources:
    LocationBundle\Entity\Location:
        itemOperations:
            get:
                method: 'GET'
                normalization_context:
                    groups: ['location-item']
        collectionOperations:
            get:
                method: 'GET'
                normalization_context:
                    groups: ['location-list']
        attributes:
            filters: ['location.search', 'location.boolean']

My provider:

class LocationCollectionDataProvider implements CollectionDataProviderInterface
{
    /**
     * @var EntityRepository
     */
    private $locationRepository;

    public function __construct(EntityRepository $locationRepository)
    {
        $this->locationRepository = $locationRepository;
    }

    public function getCollection(string $resourceClass, string $operationName = null)
    {
        if (Location::class !== $resourceClass) {
            throw new ResourceClassNotSupportedException();
        }

        return $this->locationRepository->findAll();

    }

}

services:


    location.repository:
        class: Doctrine\ORM\EntityRepository
        factory: ["@doctrine.orm.entity_manager", getRepository]
        arguments: [ LocationBundle\Entity\Location ]

    location.collection_data_provider:
        class: 'LocationBundle\DataProvider\LocationCollectionDataProvider'
        public: false
        arguments: [ "@location.repository" ]
        tags:
            -  { name: 'api_platform.collection_data_provider', priority: 2 }

Tnx! :D

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions