Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0351f6c

Browse files
committed
Support EasyAdmin 4.29.6
1 parent 55545a4 commit 0351f6c

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/Controller/BaseCrudDtoController.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
2424
use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
2525
use Override;
26+
use Protung\EasyAdminPlusBundle\Dto\EntityDtoInstanceSetter;
2627
use Psl\Str;
2728
use Symfony\Component\HttpFoundation\Response;
2829
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -98,8 +99,7 @@ public function edit(AdminContext $context): KeyValueStore|Response
9899
$entityInstance = $entityDto->getInstance();
99100

100101
$dto = $this->createDtoFromEntity($entityInstance);
101-
$entityDto->setInstance(null);
102-
$entityDto->setInstance($dto);
102+
EntityDtoInstanceSetter::setInstance($entityDto, $dto);
103103

104104
$event = new BeforeCrudActionEvent($context);
105105
$this->container->get('event_dispatcher')->dispatch($event);
@@ -156,8 +156,7 @@ public function edit(AdminContext $context): KeyValueStore|Response
156156
$this->processUploadedFiles($editForm);
157157

158158
$this->updateEntityFromDto($entityInstance, $dto);
159-
$entityDto->setInstance(null);
160-
$entityDto->setInstance($entityInstance);
159+
EntityDtoInstanceSetter::setInstance($entityDto, $entityInstance);
161160

162161
$event = new BeforeEntityUpdatedEvent($entityInstance);
163162
$this->container->get('event_dispatcher')->dispatch($event);
@@ -216,8 +215,7 @@ public function new(AdminContext $context): KeyValueStore|Response
216215
throw new InsufficientEntityPermissionException($context);
217216
}
218217

219-
$context->getEntity()->setInstance(null);
220-
$context->getEntity()->setInstance($this->createDto());
218+
EntityDtoInstanceSetter::setInstance($context->getEntity(), $this->createDto());
221219
$this->container->get(FieldFactory::class)->processFields($context->getEntity(), new FieldCollection($this->configureFields(Crud::PAGE_NEW)), Crud::PAGE_NEW);
222220
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
223221
$this->container->get(ActionFactory::class)->processEntityActions($context->getEntity(), $context->getCrud()->getActionsConfig());
@@ -227,8 +225,7 @@ public function new(AdminContext $context): KeyValueStore|Response
227225

228226
/** @var TDto $entityInstance */
229227
$entityInstance = $newForm->getData();
230-
$context->getEntity()->setInstance(null);
231-
$context->getEntity()->setInstance($entityInstance);
228+
EntityDtoInstanceSetter::setInstance($context->getEntity(), $entityInstance);
232229

233230
if ($newForm->isSubmitted() && $newForm->isValid()) {
234231
$this->processUploadedFiles($newForm);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Protung\EasyAdminPlusBundle\Dto;
6+
7+
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
8+
use ReflectionProperty;
9+
10+
final readonly class EntityDtoInstanceSetter
11+
{
12+
public static function setInstance(EntityDto $entityDto, object|null $instance): void
13+
{
14+
$instanceProperty = new ReflectionProperty($entityDto, 'instance');
15+
$instanceProperty->setValue($entityDto, $instance);
16+
17+
$primaryKeyProperty = new ReflectionProperty($entityDto, 'primaryKeyValue');
18+
$primaryKeyProperty->setValue($entityDto, null);
19+
}
20+
}

0 commit comments

Comments
 (0)