Skip to content

Commit

Permalink
disable FilterEagerLoadingsExtension for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
pmattmann committed Jan 18, 2025
1 parent c9724ff commit a4c3e74
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ services:
tags:
- 'app.input_filter'

App\Doctrine\Orm\Extension\FilterEagerLoadingsExtension:
decorates: 'api_platform.doctrine.orm.query_extension.filter_eager_loading'

App\Serializer\Normalizer\RelatedCollectionLinkNormalizer:
decorates: 'api_platform.hal.normalizer.item'
arguments:
Expand Down
47 changes: 47 additions & 0 deletions api/src/Doctrine/Orm/Extension/FilterEagerLoadingsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Doctrine\Orm\Extension;

use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use Doctrine\ORM\QueryBuilder;

final class FilterEagerLoadingsExtension implements QueryCollectionExtensionInterface {
public function __construct(private QueryCollectionExtensionInterface $decorated) {}

public function applyToCollection(
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
?string $resourceClass = null,
?Operation $operation = null,
array $context = []
): void {
// Manipulates $queryBuilder

// Orig:
// select
// from category c0_
// inner join camp c1_ ON c0_.campId = c1_.id
// ...
// where c1_.id = ?
// New:
// select ...
// from category c0_
// inner join camp c1_ ON c0_.campId = c1_.id
// ...
// where c0_.id IN (
// SELECT c9_.id
// FROM category c9_
// INNER JOIN camp c10_ ON c9_.campId = c10_.id
// WHERE c10_.id = ?
// )

// Not clear, why ApiPlatform is doing this.
// Orig-Verison performs better.
// FilterEagerLoadingExtension is disabled.

$this->decorated->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
}
}

0 comments on commit a4c3e74

Please # to comment.