-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2nd example for camp specific URL (deeply nested entity)
- Loading branch information
Showing
7 changed files
with
106 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace App\Doctrine; | ||
|
||
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface; | ||
use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface; | ||
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use App\Repository\CanFilterByCampInterface; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
final class FilterByCampExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface { | ||
private EntityManagerInterface $entityManager; | ||
|
||
public function __construct(EntityManagerInterface $entityManager) { | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass = null, Operation $operation = null, array $context = []): void { | ||
if (!isset($context['uri_variables']['campId'])) { | ||
return; | ||
} | ||
|
||
$this->addWhere($queryBuilder, $queryNameGenerator, $resourceClass, $context['uri_variables']['campId']); | ||
} | ||
|
||
public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, Operation $operation = null, array $context = []): void { | ||
if (!isset($context['uri_variables']['campId'])) { | ||
return; | ||
} | ||
|
||
$this->addWhere($queryBuilder, $queryNameGenerator, $resourceClass, $context['uri_variables']['campId']); | ||
} | ||
|
||
private function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $campId): void { | ||
$repository = $this->entityManager->getRepository($resourceClass); | ||
|
||
if (!($repository instanceof CanFilterByCampInterface) || null === $campId) { | ||
return; | ||
} | ||
|
||
$repository->filterByCamp($queryBuilder, $queryNameGenerator, $campId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Repository; | ||
|
||
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
interface CanFilterByCampInterface { | ||
public function filterByCamp(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $campId): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters