Skip to content

Commit

Permalink
Merge pull request #4943 from BacLuc/move-joins-to-subquery
Browse files Browse the repository at this point in the history
ProfileRepository: move joins to where clause
  • Loading branch information
pmattmann authored Apr 13, 2024
2 parents e87d40d + 679ec87 commit f547d9b
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 29 deletions.
8 changes: 4 additions & 4 deletions api/src/Doctrine/FilterByCurrentUserExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public function __construct(Security $security, EntityManagerInterface $entityMa
}

public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, ?string $resourceClass = null, ?Operation $operation = null, array $context = []): void {
$this->addWhere($queryBuilder, $resourceClass);
$this->addWhere($queryBuilder, $queryNameGenerator, $resourceClass);
}

public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, ?Operation $operation = null, array $context = []): void {
$this->addWhere($queryBuilder, $resourceClass);
$this->addWhere($queryBuilder, $queryNameGenerator, $resourceClass);
}

private function addWhere(QueryBuilder $queryBuilder, string $resourceClass): void {
private function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass): void {
$repository = $this->entityManager->getRepository($resourceClass);

/** @var null|User $user */
Expand All @@ -39,6 +39,6 @@ private function addWhere(QueryBuilder $queryBuilder, string $resourceClass): vo
return;
}

$repository->filterByUser($queryBuilder, $user);
$repository->filterByUser($queryBuilder, $queryNameGenerator, $user);
}
}
3 changes: 2 additions & 1 deletion api/src/Repository/ActivityProgressLabelRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\ActivityProgressLabel;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -27,7 +28,7 @@ public function __construct(EntityManagerInterface $em, string $entityClass = Ac
parent::__construct($em, $entityClass);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->innerJoin("{$rootAlias}.camp", 'camp');
$this->filterByCampCollaboration($queryBuilder, $user);
Expand Down
3 changes: 2 additions & 1 deletion api/src/Repository/ActivityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\Activity;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
Expand All @@ -21,7 +22,7 @@ public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, Activity::class);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->innerJoin("{$rootAlias}.camp", 'camp');
$this->filterByCampCollaboration($queryBuilder, $user);
Expand Down
3 changes: 2 additions & 1 deletion api/src/Repository/ActivityResponsibleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\ActivityResponsible;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
Expand All @@ -21,7 +22,7 @@ public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, ActivityResponsible::class);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->innerJoin("{$rootAlias}.activity", 'activity');
$queryBuilder->innerJoin('activity.camp', 'camp');
Expand Down
3 changes: 2 additions & 1 deletion api/src/Repository/CampCollaborationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\Camp;
use App\Entity\CampCollaboration;
use App\Entity\User;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function findAllByPersonallyInvitedUser(User $user): array {
return $this->findBy(['user' => $user, 'status' => CampCollaboration::STATUS_INVITED]);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->innerJoin("{$rootAlias}.camp", 'camp');
$this->filterByCampCollaboration($queryBuilder, $user);
Expand Down
3 changes: 2 additions & 1 deletion api/src/Repository/CampRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\Camp;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
Expand All @@ -21,7 +22,7 @@ public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, Camp::class);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
/** @var string $rootAlias */
$rootAlias = $queryBuilder->getRootAliases()[0];
$this->filterByCampCollaboration($queryBuilder, $user, $rootAlias);
Expand Down
3 changes: 2 additions & 1 deletion api/src/Repository/CanFilterByUserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\User;
use Doctrine\ORM\QueryBuilder;

interface CanFilterByUserInterface {
public function filterByUser(QueryBuilder $queryBuilder, User $user): void;
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void;
}
3 changes: 2 additions & 1 deletion api/src/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\Category;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
Expand All @@ -21,7 +22,7 @@ public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, Category::class);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->innerJoin("{$rootAlias}.camp", 'camp');
$this->filterByCampCollaboration($queryBuilder, $user);
Expand Down
3 changes: 2 additions & 1 deletion api/src/Repository/ContentNodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\ContentNode;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -27,7 +28,7 @@ public function __construct(EntityManagerInterface $em, string $entityClass = Co
parent::__construct($em, $entityClass);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$this->filterByContentNode($queryBuilder, $user, $queryBuilder->getRootAliases()[0]);
}
}
3 changes: 2 additions & 1 deletion api/src/Repository/DayRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\Day;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
Expand All @@ -21,7 +22,7 @@ public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, Day::class);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->innerJoin("{$rootAlias}.period", 'period');
$queryBuilder->innerJoin('period.camp', 'camp');
Expand Down
3 changes: 2 additions & 1 deletion api/src/Repository/DayResponsibleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Doctrine\QueryBuilderHelper;
use App\Entity\Day;
use App\Entity\DayResponsible;
Expand All @@ -23,7 +24,7 @@ public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, DayResponsible::class);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];

$dayQry = $queryBuilder->getEntityManager()->createQueryBuilder();
Expand Down
3 changes: 2 additions & 1 deletion api/src/Repository/MaterialItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\MaterialItem;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
Expand All @@ -21,7 +22,7 @@ public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, MaterialItem::class);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->innerJoin("{$rootAlias}.materialList", 'materialList');
$queryBuilder->innerJoin('materialList.camp', 'camp');
Expand Down
3 changes: 2 additions & 1 deletion api/src/Repository/MaterialListRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\MaterialList;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
Expand All @@ -21,7 +22,7 @@ public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, MaterialList::class);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->innerJoin("{$rootAlias}.camp", 'camp');
$this->filterByCampCollaboration($queryBuilder, $user);
Expand Down
3 changes: 2 additions & 1 deletion api/src/Repository/PeriodRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\Period;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
Expand All @@ -21,7 +22,7 @@ public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, Period::class);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->innerJoin("{$rootAlias}.camp", 'camp');
$this->filterByCampCollaboration($queryBuilder, $user);
Expand Down
43 changes: 32 additions & 11 deletions api/src/Repository/ProfileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\CampCollaboration;
use App\Entity\Profile;
use App\Entity\User;
Expand All @@ -21,19 +22,39 @@ public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, Profile::class);
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->join("{$rootAlias}.user", 'user');
$queryBuilder->leftJoin('user.collaborations', 'userCampCollaborations');
$queryBuilder->leftJoin('userCampCollaborations.camp', 'camp');
$queryBuilder->leftJoin('camp.collaborations', 'relatedCampCollaborations');
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$expr = new Expr();

$relatedUserSubQueryBuilder = $this->getEntityManager()->createQueryBuilder();

$innerUserAlias = $queryNameGenerator->generateJoinAlias('user');
$userCollaborationsAlias = $queryNameGenerator->generateJoinAlias('collaborations');
$campAlias = $queryNameGenerator->generateJoinAlias('camp');
$relatedCollaborationsAlias = $queryNameGenerator->generateJoinAlias('collaborations');

$relatedUserQuery = $relatedUserSubQueryBuilder
->select($innerUserAlias)
->from(User::class, $innerUserAlias)
->join("{$innerUserAlias}.collaborations", $userCollaborationsAlias)
->join("{$userCollaborationsAlias}.camp", $campAlias)
->join("{$campAlias}.collaborations", $relatedCollaborationsAlias)
->where(
$expr->andX(
$expr->eq("{$userCollaborationsAlias}.status", ':status_established'),
$expr->eq("{$relatedCollaborationsAlias}.status", ':status_established'),
$expr->eq("{$relatedCollaborationsAlias}.user", ' :current_user'),
)
)
;

$rootAlias = $queryBuilder->getRootAliases()[0];
$userAlias = $queryNameGenerator->generateJoinAlias('user');
$queryBuilder->join("{$rootAlias}.user", $userAlias);
$queryBuilder->andWhere($expr->orX(
$expr->eq('user', ':current_user'),
$expr->andX(
$expr->eq('userCampCollaborations.status', ':status_established'),
$expr->eq('relatedCampCollaborations.status', ':status_established'),
$expr->eq('relatedCampCollaborations.user', ' :current_user'),
$expr->eq($userAlias, ':current_user'),
$expr->in(
$userAlias,
$relatedUserQuery->getDQL()
)
));
$queryBuilder->setParameter('current_user', $user);
Expand Down
3 changes: 2 additions & 1 deletion api/src/Repository/ScheduleEntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository;

use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\ScheduleEntry;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
Expand Down Expand Up @@ -33,7 +34,7 @@ public function createQueryBuilder($alias, $indexBy = null): QueryBuilder {
return $qb;
}

public function filterByUser(QueryBuilder $queryBuilder, User $user): void {
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->innerJoin("{$rootAlias}.activity", 'activity');
$queryBuilder->innerJoin('activity.camp', 'camp');
Expand Down

0 comments on commit f547d9b

Please sign in to comment.