Skip to content

Commit c82d829

Browse files
committed
Added ORM 3.4 property hook support
1 parent 2857052 commit c82d829

File tree

6 files changed

+55
-19
lines changed

6 files changed

+55
-19
lines changed

Controller/Crud.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Arkounay\Bundle\QuickAdminGeneratorBundle\Controller;
55

6+
use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\EntityService;
67
use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\FieldService;
78
use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\RouteExtension;
89
use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\TwigLoaderService;
@@ -56,6 +57,7 @@ abstract class Crud extends AbstractController
5657
protected TranslatorInterface $translator;
5758
protected TwigLoaderService $twigLoader;
5859
protected QagExtensionRuntime $qagExtensionRuntime;
60+
protected EntityService $entityService;
5961

6062
/** @var EntityRepository<T> */
6163
protected EntityRepository $repository;
@@ -88,7 +90,8 @@ public function setInternalDependencies(
8890
TranslatorInterface $translator,
8991
TwigLoaderService $twigLoader,
9092
SluggerInterface $slugger,
91-
QagExtensionRuntime $qagExtensionRuntime
93+
QagExtensionRuntime $qagExtensionRuntime,
94+
EntityService $entityService
9295
): void {
9396
$this->em = $em;
9497
$this->fieldService = $fieldService;
@@ -100,6 +103,7 @@ public function setInternalDependencies(
100103
$this->repository = $em->getRepository($this->getEntity());
101104
$this->slugger = $slugger;
102105
$this->qagExtensionRuntime = $qagExtensionRuntime;
106+
$this->entityService = $entityService;
103107
}
104108

105109
/**
@@ -323,7 +327,7 @@ public function toggleBooleanPostAction(Request $request, $entity): Response
323327
if (!$this->isEditableBoolean($entity)) {
324328
throw $this->createAccessDeniedException("Entity {$this->getEntity()} cannot be edited (boolean).");
325329
}
326-
330+
327331
$index = $request->request->get('index');
328332
$value = $request->request->getBoolean('checked');
329333
$propertyAccessor = PropertyAccess::createPropertyAccessor();
@@ -637,7 +641,7 @@ protected function updateEntity($entity, bool $creation): void
637641
if ($event->isPropagationStopped()) {
638642
return;
639643
}
640-
$this->addFlash('highlighted_row_id', $entity->getId());
644+
$this->addFlash('highlighted_row_id', $this->entityService->getId($entity));
641645
}
642646

643647
/**
@@ -1090,7 +1094,7 @@ protected function entityIsInList($entity): bool
10901094
{
10911095
return $this->getListQueryBuilder()
10921096
->andWhere('e.id = :id')
1093-
->setParameter('id', $entity->getId())
1097+
->setParameter('id', $this->entityService->getId($entity))
10941098
->getQuery()
10951099
->getOneOrNullResult() !== null;
10961100
}
@@ -1158,7 +1162,7 @@ public function checkSecurity(string $action, $entity = null): void
11581162
throw $this->createAccessDeniedException("Entity {$this->getEntity()} cannot be edited.");
11591163
}
11601164
if ($this->hasQuickListQueryBuilderSecurity() && !$this->entityIsInList($entity)) {
1161-
throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$entity->getId()} is filtered out.");
1165+
throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$this->entityService->getId($entity)} is filtered out.");
11621166
}
11631167
if ($entity === null) {
11641168
throw $this->createNotFoundException("No {$this->getNameSentence()} found with id #{$this->request->attributes->get('id')}");
@@ -1174,15 +1178,15 @@ public function checkSecurity(string $action, $entity = null): void
11741178
throw $this->createAccessDeniedException("Entity {$this->getEntity()} is not removable.");
11751179
}
11761180
if ($this->hasQuickListQueryBuilderSecurity() && !$this->entityIsInList($entity)) {
1177-
throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$entity->getId()} is filtered out.");
1181+
throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$this->entityService->getId($entity)} is filtered out.");
11781182
}
11791183
break;
11801184
case 'view':
11811185
if (!$this->isViewable($entity)) {
11821186
throw $this->createAccessDeniedException("Entity {$this->getEntity()} cannot be viewed.");
11831187
}
11841188
if ($this->hasQuickListQueryBuilderSecurity() && !$this->entityIsInList($entity)) {
1185-
throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$entity->getId()} is filtered out.");
1189+
throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$this->entityService->getId($entity)} is filtered out.");
11861190
}
11871191
if ($entity === null) {
11881192
throw $this->createNotFoundException("No {$this->getName()} found with id #{$this->request->attributes->get('id')}");
@@ -1193,7 +1197,7 @@ public function checkSecurity(string $action, $entity = null): void
11931197
throw $this->createAccessDeniedException("Entity {$this->getEntity()} cannot be edited.");
11941198
}
11951199
if ($this->hasQuickListQueryBuilderSecurity() && !$this->entityIsInList($entity)) {
1196-
throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$entity->getId()} is filtered out.");
1200+
throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$this->entityService->getId($entity)} is filtered out.");
11971201
}
11981202
break;
11991203
}
@@ -1202,4 +1206,4 @@ public function checkSecurity(string $action, $entity = null): void
12021206
$this->eventDispatcher->dispatch($event, 'qag.events.security');
12031207
}
12041208

1205-
}
1209+
}

Controller/GlobalSearchController.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Arkounay\Bundle\QuickAdminGeneratorBundle\Controller;
55

6+
use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\EntityService;
67
use Doctrine\ORM\Tools\Pagination\Paginator;
78
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
89
use Symfony\Component\EventDispatcher\GenericEvent;
@@ -18,7 +19,8 @@ public function __construct(
1819
/** @var iterable|Crud[] */
1920
private readonly iterable $cruds,
2021
private readonly EventDispatcherInterface $eventDispatcher,
21-
private readonly TranslatorInterface $translator
22+
private readonly TranslatorInterface $translator,
23+
private readonly EntityService $entityService
2224
)
2325
{}
2426

@@ -64,9 +66,9 @@ private function getSearchResults(Request $request, int $maxResults = 5): array
6466
if (\count($actions) > 0) {
6567
$url = null;
6668
if ($crud->isViewable($entity)) {
67-
$url = $this->generateUrl("qag.{$crud->getRoute()}_view", ['id' => $entity->getId(), 'highlight' => $query]);
69+
$url = $this->generateUrl("qag.{$crud->getRoute()}_view", ['id' => $this->entityService->getId($entity), 'highlight' => $query]);
6870
} elseif ($crud->isEditable($entity)) {
69-
$url = $this->generateUrl("qag.{$crud->getRoute()}_edit", ['id' => $entity->getId(), 'highlight' => $query]);
71+
$url = $this->generateUrl("qag.{$crud->getRoute()}_edit", ['id' => $this->entityService->getId($entity), 'highlight' => $query]);
7072
}
7173

7274
if ($url !== null) {
@@ -108,4 +110,4 @@ private function getSearchResults(Request $request, int $maxResults = 5): array
108110
return $searchResult;
109111
}
110112

111-
}
113+
}

DependencyInjection/ArkounayQuickAdminGeneratorExtension.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
use Arkounay\Bundle\QuickAdminGeneratorBundle\Controller\Crud;
8+
use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\EntityService;
89
use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\FieldService;
910
use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\TwigLoaderService;
1011
use Arkounay\Bundle\QuickAdminGeneratorBundle\Twig\Runtime\QagExtensionRuntime;
@@ -44,9 +45,10 @@ public function load(array $configs, ContainerBuilder $container): void
4445
new Reference(TranslatorInterface::class),
4546
new Reference(TwigLoaderService::class),
4647
new Reference(SluggerInterface::class),
47-
new Reference(QagExtensionRuntime::class)
48+
new Reference(QagExtensionRuntime::class),
49+
new Reference(EntityService::class)
4850
]);
4951

5052
}
5153

52-
}
54+
}

Extension/EntityService.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Arkounay\Bundle\QuickAdminGeneratorBundle\Extension;
4+
5+
use Doctrine\ORM\EntityManagerInterface;
6+
7+
readonly class EntityService
8+
{
9+
10+
public function __construct(private EntityManagerInterface $em) {}
11+
12+
public function getId(object $entity): mixed
13+
{
14+
$metadata = $this->em->getClassMetadata(get_class($entity));
15+
$idField = $metadata->getSingleIdentifierFieldName();
16+
return $metadata->getFieldValue($entity, $idField);
17+
}
18+
19+
}

Resources/config/services.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ services:
1515
arguments:
1616
$formRenderer: '@twig.form.renderer'
1717

18+
Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\EntityService:
19+
autowire: true
20+
1821
Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\TwigLoaderService:
1922
autowire: true
2023

@@ -67,4 +70,4 @@ services:
6770
arguments:
6871
$projectDir: '%kernel.project_dir%'
6972
$cruds: !tagged_iterator quickadmin.crud
70-
autowire: true
73+
autowire: true

Twig/Runtime/QagExtensionRuntime.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\HttpFoundation\RequestStack;
1111
use Symfony\Component\Routing\RouterInterface;
1212
use Twig\Environment;
13+
use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\EntityService;
1314
use Twig\Extension\RuntimeExtensionInterface;
1415

1516
class QagExtensionRuntime implements RuntimeExtensionInterface
@@ -22,9 +23,10 @@ public function __construct(
2223
private readonly RequestStack $requestStack,
2324
private readonly MenuInterface $menu,
2425
private readonly Environment $twig,
25-
private readonly EventDispatcherInterface $eventDispatcher
26+
private readonly EventDispatcherInterface $eventDispatcher,
27+
private readonly EntityService $entityService
2628
) {}
27-
29+
2830
private function getMenuItems(): iterable
2931
{
3032
return $this->menu->generateMenu();
@@ -44,7 +46,7 @@ public function getActionHref(Action $action, $entity = null): string
4446
$params = $_GET; /* use $_GET instead of $request->query->all() to avoid knp pagination's change of query attributes */
4547
unset($params['referer']);
4648
if ($entity !== null) {
47-
$params['id'] = $entity->getId();
49+
$params['id'] = $this->entityService->getId($entity);
4850
if ($request->attributes->get('qag.from') === 'view') {
4951
$params['from'] = 'view';
5052
}
@@ -90,4 +92,8 @@ public function getQag(): array
9092
return $this->_cache;
9193
}
9294

95+
protected function getEntityIdentifier() {
96+
97+
}
98+
9399
}

0 commit comments

Comments
 (0)