diff --git a/README.md b/README.md index c62348d..2f76e3b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Inspired from https://github.com/api-platform/demo -There is tree examples in this repo +There is four examples in this repo ## First Example use raw data from a csv file @@ -52,6 +52,22 @@ The param `order` can be `title` or `id` and ordered by `asc|desc`. The paginati data provider using repository (by group from normalization_context on Movie entity) `api/movies/custom-action-using-dataprovider?page=2&order[title]=desc&isPublished=false` +## Fourth example use QueryBuilder in CarCollectionDataProvider + +This example show how use queryBuilder in CarCollectionDataProvider and filter by color. The pagination is available + +### Usage + +`/api/cars?color=color_name` + +#### Color name available + +- red +- orange +- green +- yellow +- black + ## Install composer install diff --git a/fixtures/car.yaml b/fixtures/car.yaml new file mode 100644 index 0000000..03830c5 --- /dev/null +++ b/fixtures/car.yaml @@ -0,0 +1,4 @@ +App\Entity\Car: + movie_{1..1000}: + name: + color: '' diff --git a/src/DataProvider/CarCollectionDataProvider.php b/src/DataProvider/CarCollectionDataProvider.php new file mode 100644 index 0000000..7c1c1a0 --- /dev/null +++ b/src/DataProvider/CarCollectionDataProvider.php @@ -0,0 +1,52 @@ +managerRegistry = $managerRegistry; + $this->paginationExtension = $paginationExtension; + } + + public function supports(string $resourceClass, string $operationName = null, array $context = []): bool + { + return Car::class === $resourceClass; + } + + public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable + { + $queryBuilder = $this->managerRegistry + ->getManagerForClass($resourceClass) + ->getRepository($resourceClass)->createQueryBuilder('c'); + + if (isset($context['filters']['color'])) { + $queryBuilder + ->where('c.color = :color') + ->setParameter('color', $context['filters']['color']) + ; + } + + $this->paginationExtension->applyToCollection($queryBuilder, new QueryNameGenerator(), $resourceClass, $operationName, $context); + + if ($this->paginationExtension instanceof QueryResultCollectionExtensionInterface && + $this->paginationExtension->supportsResult($resourceClass, $operationName, $context)) { + return $this->paginationExtension->getResult($queryBuilder, $resourceClass, $operationName, $context); + } + + return $queryBuilder->getQuery()->getResult(); + + } +} diff --git a/src/Entity/Car.php b/src/Entity/Car.php new file mode 100644 index 0000000..d9676a8 --- /dev/null +++ b/src/Entity/Car.php @@ -0,0 +1,60 @@ +id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + public function getColor(): ?string + { + return $this->color; + } + + public function setColor(string $color): self + { + $this->color = $color; + + return $this; + } +} diff --git a/src/Repository/CarRepository.php b/src/Repository/CarRepository.php new file mode 100644 index 0000000..b75c933 --- /dev/null +++ b/src/Repository/CarRepository.php @@ -0,0 +1,50 @@ +createQueryBuilder('c') + ->andWhere('c.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('c.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /* + public function findOneBySomeField($value): ?Car + { + return $this->createQueryBuilder('c') + ->andWhere('c.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +}