question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apply filters in custom data provider

See original GitHub issue

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! 😄

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
devrckcommented, Aug 30, 2017

Hi,

I’m also interested in this issue because it’s not explained in the docs how you can create a custom filter and apply it to a custom data provider. You have only an example where you create a custom filter but it’s tied to doctrine.

1reaction
fulopattila122commented, Jun 4, 2018

I’ve found that for Symfony 3.4+ it can be added to the service definition this way:

services:
    AppBundle\DataProvider\ShopContentLibraryItemProvider:
        tags: [ { name: 'api_platform.collection_data_provider', priority: 3 } ]
        autoconfigure: false
        arguments:
            $collectionExtensions: !tagged api_platform.doctrine.orm.query_extension.collection

Unfortunately the project is on Symfony 3.3, so apparently I have to create a compiler pass 🙄

Read more comments on GitHub >

github_iconTop Results From Across the Web

API-Platform: Filtering Custom Data Provider - Stack Overflow
It was as simple as creating my own version of the SearchFilter , implementing ApiPlatform\Core\Api\FilterInterface . <?php namespace App\Filter ...
Read more >
Custom Filter apply() > API Platform Part 3 - SymfonyCasts
Let's look at how this is done in the core Doctrine data provider. Hit Shift+Shift, search for doctrinedataprovider and include non project items....
Read more >
Create filter data providers for PerformancePoint Services in ...
In PerformancePoint Services, custom data providers retrieve data from a filter's underlying data source and define how to use the data.
Read more >
To view and filter the data in a data provider
To filter the data, click the arrow on a column header and do one of the following: Select a value from the dropdown...
Read more >
Filter pages by a property using a custom SmartContent ...
Filter pages by a property using a custom SmartContent DataProvider¶ · 1. Implement a custom QueryBuilder class that adds conditions. <? · 2....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found