Skip to content

Commit 4d8d22a

Browse files
author
Aymeric Ratinaud
committed
QueryBuilder in CarCollectionDataProvider
1 parent 26560aa commit 4d8d22a

File tree

5 files changed

+183
-1
lines changed

5 files changed

+183
-1
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Inspired from https://github.com/api-platform/demo
44

5-
There is tree examples in this repo
5+
There is four examples in this repo
66

77
## First Example use raw data from a csv file
88

@@ -52,6 +52,22 @@ The param `order` can be `title` or `id` and ordered by `asc|desc`. The paginati
5252
data provider using repository (by group from normalization_context on Movie entity)
5353
`api/movies/custom-action-using-dataprovider?page=2&order[title]=desc&isPublished=false`
5454

55+
## Fourth example use QueryBuilder in CarCollectionDataProvider
56+
57+
This example show how use queryBuilder in CarCollectionDataProvider and filter by color. The pagination is available
58+
59+
### Usage
60+
61+
`/api/cars?color=color_name`
62+
63+
#### Color name available
64+
65+
- red
66+
- orange
67+
- green
68+
- yellow
69+
- black
70+
5571
## Install
5672

5773
composer install

fixtures/car.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
App\Entity\Car:
2+
movie_{1..1000}:
3+
name: <word(10, 700)>
4+
color: '<randomElement( ["red", "orange", "green", "yellow", "black"] )>'
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace App\DataProvider;
4+
5+
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\PaginationExtension;
6+
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
7+
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
8+
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
9+
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
10+
use App\Entity\Car;
11+
use Doctrine\Persistence\ManagerRegistry;
12+
13+
final class CarCollectionDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface
14+
{
15+
private $managerRegistry;
16+
private $paginationExtension;
17+
18+
public function __construct(ManagerRegistry $managerRegistry, PaginationExtension $paginationExtension)
19+
{
20+
$this->managerRegistry = $managerRegistry;
21+
$this->paginationExtension = $paginationExtension;
22+
}
23+
24+
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
25+
{
26+
return Car::class === $resourceClass;
27+
}
28+
29+
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
30+
{
31+
$queryBuilder = $this->managerRegistry
32+
->getManagerForClass($resourceClass)
33+
->getRepository($resourceClass)->createQueryBuilder('c');
34+
35+
if (isset($context['filters']['color'])) {
36+
$queryBuilder
37+
->where('c.color = :color')
38+
->setParameter('color', $context['filters']['color'])
39+
;
40+
}
41+
42+
$this->paginationExtension->applyToCollection($queryBuilder, new QueryNameGenerator(), $resourceClass, $operationName, $context);
43+
44+
if ($this->paginationExtension instanceof QueryResultCollectionExtensionInterface &&
45+
$this->paginationExtension->supportsResult($resourceClass, $operationName, $context)) {
46+
return $this->paginationExtension->getResult($queryBuilder, $resourceClass, $operationName, $context);
47+
}
48+
49+
return $queryBuilder->getQuery()->getResult();
50+
51+
}
52+
}

src/Entity/Car.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use ApiPlatform\Core\Annotation\ApiResource;
6+
use App\Repository\CarRepository;
7+
use Doctrine\ORM\Mapping as ORM;
8+
9+
/**
10+
* @ApiResource()
11+
* @ORM\Entity(repositoryClass=CarRepository::class)
12+
*/
13+
class Car
14+
{
15+
/**
16+
* @ORM\Id
17+
* @ORM\GeneratedValue
18+
* @ORM\Column(type="integer")
19+
*/
20+
private $id;
21+
22+
/**
23+
* @ORM\Column(type="string", length=255)
24+
*/
25+
private $name;
26+
27+
/**
28+
* @ORM\Column(type="string", length=255)
29+
*/
30+
private $color;
31+
32+
public function getId(): ?int
33+
{
34+
return $this->id;
35+
}
36+
37+
public function getName(): ?string
38+
{
39+
return $this->name;
40+
}
41+
42+
public function setName(string $name): self
43+
{
44+
$this->name = $name;
45+
46+
return $this;
47+
}
48+
49+
public function getColor(): ?string
50+
{
51+
return $this->color;
52+
}
53+
54+
public function setColor(string $color): self
55+
{
56+
$this->color = $color;
57+
58+
return $this;
59+
}
60+
}

src/Repository/CarRepository.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Repository;
4+
5+
use App\Entity\Car;
6+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7+
use Doctrine\Persistence\ManagerRegistry;
8+
9+
/**
10+
* @method Car|null find($id, $lockMode = null, $lockVersion = null)
11+
* @method Car|null findOneBy(array $criteria, array $orderBy = null)
12+
* @method Car[] findAll()
13+
* @method Car[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
14+
*/
15+
class CarRepository extends ServiceEntityRepository
16+
{
17+
public function __construct(ManagerRegistry $registry)
18+
{
19+
parent::__construct($registry, Car::class);
20+
}
21+
22+
// /**
23+
// * @return Car[] Returns an array of Car objects
24+
// */
25+
/*
26+
public function findByExampleField($value)
27+
{
28+
return $this->createQueryBuilder('c')
29+
->andWhere('c.exampleField = :val')
30+
->setParameter('val', $value)
31+
->orderBy('c.id', 'ASC')
32+
->setMaxResults(10)
33+
->getQuery()
34+
->getResult()
35+
;
36+
}
37+
*/
38+
39+
/*
40+
public function findOneBySomeField($value): ?Car
41+
{
42+
return $this->createQueryBuilder('c')
43+
->andWhere('c.exampleField = :val')
44+
->setParameter('val', $value)
45+
->getQuery()
46+
->getOneOrNullResult()
47+
;
48+
}
49+
*/
50+
}

0 commit comments

Comments
 (0)