Apply filters in custom data provider
See original GitHub issueHi, 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:
- Created 6 years ago
- Reactions:3
- Comments:10 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
I’ve found that for Symfony 3.4+ it can be added to the service definition this way:
Unfortunately the project is on Symfony 3.3, so apparently I have to create a compiler pass 🙄