Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony] Api Platform - domyślna wartość w ApiFilter
czachor
post 15.09.2020, 15:52:43
Post #1





Grupa: Zarejestrowani
Postów: 897
Pomógł: 40
Dołączył: 16.12.2003
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Mam taką encję:

  1. /**
  2.  * @ApiFilter(BooleanFilter::class, properties={"isActive"})
  3.  * @ORM\Entity(repositoryClass=FooRepository::class)
  4.  */
  5. class Foo
  6. {
  7. /**
  8.   * @ORM\Column(type="boolean")
  9.   */
  10. private bool $isActive = true;
  11.  
  12. // ciach reszty, nie dotyczy problemu
  13. }


Korzystam z paczki ApiPlatform, w której wygenerowałem sobie całe API. Mam endpoint `/api/foo`, w którym wypluwam listę elementów. Powyższe rozwiązanie pozwala na wybór w requeście, czy $isActive ma być true / false. Chciałbym mieć wymuszone filtrowanie, żeby API zwróciło tylko elementy, które mają wartość `$isActive` równą `true` - bez możliwości wyboru. Jak to osiągnąć?

Ten post edytował czachor 15.09.2020, 15:53:41


--------------------
how many SEO experts does it take to change a light bulb,lightbulb,light,bulb,lamp,lighting,switch,sex,xxx
5-Reasons-why-you-should-NEVER-fix-a-computer-for-free
Go to the top of the page
+Quote Post
ohm
post 15.09.2020, 16:24:08
Post #2





Grupa: Zarejestrowani
Postów: 618
Pomógł: 143
Dołączył: 22.12.2010

Ostrzeżenie: (0%)
-----


O ile mnie pamiec nie myli, to przez dataprovider modyfikujesz zapytanie.
Go to the top of the page
+Quote Post
czachor
post 16.09.2020, 10:42:02
Post #3





Grupa: Zarejestrowani
Postów: 897
Pomógł: 40
Dołączył: 16.12.2003
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Dzięki, faktycznie chodzi o DataProvider.
Manual: https://api-platform.com/docs/core/data-providers/

I rozwiązanie (oczywiście metodę 'findByFieldBar()' trzeba juz sobie samemu ogarnąć w Repository:
  1. <?php
  2.  
  3. namespace App\DataProvider;
  4.  
  5. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  6. use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
  7. use App\Entity\Foo;
  8. use App\Repository\FooRepository;
  9.  
  10. final class FooCollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
  11. {
  12. /**
  13.   * @var FooRepository
  14.   */
  15. private FooRepository $fooRepository;
  16.  
  17. public function __construct(FooRepository $fooRepository)
  18. {
  19. $this->fooRepository = $fooRepository;
  20. }
  21.  
  22. public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
  23. {
  24. return Foo::class === $resourceClass;
  25. }
  26.  
  27. public function getCollection(string $resourceClass, string $operationName = null)
  28. {
  29.  
  30. return $this->fooRepository->findByFieldBar();
  31. }
  32. }


--------------------
how many SEO experts does it take to change a light bulb,lightbulb,light,bulb,lamp,lighting,switch,sex,xxx
5-Reasons-why-you-should-NEVER-fix-a-computer-for-free
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 15:49