Skip to content

[Doctrine] Allow custom repository method instead of createQueryBuilder #7976

@giosh94mhz

Description

@giosh94mhz

Description

In our codebase we use a different createQueryBuilder, named createUserFilteredQueryBuilder, to make sure that no queries is generated without global constraint.

Since ApiPlatform enforce using a specific method we are forced to a lot of DI customization to override the limitation.

Possible Implementation

First thing first: I will provide a MR for this, but I'd like a feedback before start.

This is the line that need improvements:

public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable
{
$entityClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);
/** @var EntityManagerInterface $manager */
$manager = $this->managerRegistry->getManagerForClass($entityClass);
$repository = $manager->getRepository($entityClass);
if (!method_exists($repository, 'createQueryBuilder')) {
throw new RuntimeException('The repository class must have a "createQueryBuilder" method.');
}
$queryBuilder = $repository->createQueryBuilder('o');

(Same for Item Providers and ODM Providers)

If I understand the code correctly, I have to add a nullable option createQueryBuilderMethod to ApiPlatform\Doctrine\Common\State\Options, and than fallback to createQueryBuilder where it's used in the current codebase.

My questions are:

  1. Am I correct that "state options" is the right place? If so, can you confirm that no changes is required to the API metadata (and factories)?
  2. Have you any recommendation for testing this changes? I see only the CollectionProviderTests and ItemProviderTest that seems related

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions