Skip to content

Commit

Permalink
Fixed QueryBuilder aliases for case-insensitive environments
Browse files Browse the repository at this point in the history
  • Loading branch information
jzaplet committed Mar 13, 2024
1 parent bbecad4 commit 5eb5e9d
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/Database/Manager/AuthResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ public function collectionNavResources(): array
private function removeResources(array $names): void
{
$this->em->getAuthResourceRepo()
->createQueryBuilder('Resource')
->createQueryBuilder('resource')
->delete()
->andWhere('Resource.name IN (:names)')
->andWhere('resource.name IN (:names)')
->setParameter('names', $names)
->getQuery()->execute();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Request/Auth/RevokeTokenRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public function schema(): array

public function process(array $data): Response
{
$this->em->getAuthTokenRepo()->createQueryBuilder('Token')
$this->em->getAuthTokenRepo()->createQueryBuilder('token')
->delete()
->where('Token.source = :source')
->andWhere('Token.sourceId IN (:source_ids)')
->where('token.source = :source')
->andWhere('token.sourceId IN (:source_ids)')
->setParameter('source_ids', $data['source_ids'])
->setParameter('source', $data['source'])
->getQuery()
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Request/Collection/DeleteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public function process(array $data): Response

$repo = $this->em->getRepository($meta->className);

$qb = $repo->createQueryBuilder('E')
->where('E.id IN (:ids)')
$qb = $repo->createQueryBuilder('entity')
->where('entity.id IN (:ids)')
->setParameter('ids', $data['ids']);

$countRows = (int)(clone $qb)->select('count(E.id)')->getQuery()->getSingleScalarResult();
$countRows = (int)(clone $qb)->select('count(entity.id)')->getQuery()->getSingleScalarResult();
$countItems = count($data['ids']);
$diff = $countItems - $countRows;

Expand Down
6 changes: 3 additions & 3 deletions src/Http/Request/Collection/ShowOneRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public function process(array $data): Response

$repo = $this->em->getRepository($meta->className);

$qb = $repo->createQueryBuilder('E')
->select($meta->getQuerySelect('E'))
->where('E.id = :id')
$qb = $repo->createQueryBuilder('entity')
->select($meta->getQuerySelect('entity'))
->where('entity.id = :id')
->setParameter('id', $data['id']);

$item = $qb->getQuery()->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY);
Expand Down
8 changes: 4 additions & 4 deletions src/Http/Request/Collection/ShowRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ public function process(array $data): Response

$repo = $this->em->getRepository($meta->className);

$qb = $repo->createQueryBuilder('E')
->select($meta->getQuerySelect('E'));
$qb = $repo->createQueryBuilder('entity')
->select($meta->getQuerySelect('entity'));

$count = (clone $qb)->select('count(E.id)')->getQuery()->getSingleScalarResult();
$count = (clone $qb)->select('count(entity.id)')->getQuery()->getSingleScalarResult();

$qb->setFirstResult(($data['currentPage'] - 1) * $data['itemsPerPage'])
->setMaxResults($data['itemsPerPage']);

foreach ($data['orderBy'] as $param) {
$qb->addOrderBy("E.{$param['col']}", $param['desc'] ? 'DESC' : 'ASC');
$qb->addOrderBy("entity.{$param['col']}", $param['desc'] ? 'DESC' : 'ASC');
}

$result = [
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Request/Collection/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public function process(array $data): Response
$ids = array_map(fn($row) => $row['id'], $data['rows']);

$qb = $this->em->getRepository($meta->className)
->createQueryBuilder('E')
->select('E')
->where('E.id IN (:ids)')
->createQueryBuilder('entity')
->select('entity')
->where('entity.id IN (:ids)')
->setParameter('ids', $ids);

/** @var \Megio\Database\Interface\ICrudable[] $rows */
Expand Down
8 changes: 4 additions & 4 deletions src/Http/Request/Resource/ShowAllRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public function process(array $data): Response
$resources = $this->em->getAuthResourceRepo()->findBy([], ['type' => 'ASC', 'name' => 'ASC']);

/** @var \Megio\Database\Entity\Auth\Role[] $roles */
$roles = $this->em->getAuthRoleRepo()->createQueryBuilder('Role')
->select('Role', 'Resource')
->leftJoin('Role.resources', 'Resource')
->orderBy('Role.createdAt', 'ASC')
$roles = $this->em->getAuthRoleRepo()->createQueryBuilder('role')
->select('role', 'resource')
->leftJoin('role.resources', 'resource')
->orderBy('role.createdAt', 'ASC')
->getQuery()
->getResult();

Expand Down
14 changes: 7 additions & 7 deletions src/Subscriber/AuthRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ public function onRequest(RequestEvent $event): void
/** @var class-string $className */
$userRepo = $this->em->getRepository($className);

$qb = $userRepo->createQueryBuilder('User')
->select('User')
->andWhere('User.id = :source_id')
$qb = $userRepo->createQueryBuilder('user')
->select('user')
->andWhere('user.id = :source_id')
->setParameter('source_id', $token->getSourceId());

if ($className !== Admin::class) {
$qb
->addSelect('Role')
->addSelect('Resource')
->leftJoin('User.roles', 'Role')
->leftJoin('Role.resources', 'Resource');
->addSelect('role')
->addSelect('resource')
->leftJoin('user.roles', 'role')
->leftJoin('role.resources', 'resource');
}

$user = $qb->getQuery()->getOneOrNullResult();
Expand Down

0 comments on commit 5eb5e9d

Please sign in to comment.