Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/Controller/BaseCrudDtoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
use Override;
use Protung\EasyAdminPlusBundle\Dto\EntityDtoInstanceSetter;
use Psl\Str;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
Expand Down Expand Up @@ -98,8 +99,7 @@ public function edit(AdminContext $context): KeyValueStore|Response
$entityInstance = $entityDto->getInstance();

$dto = $this->createDtoFromEntity($entityInstance);
$entityDto->setInstance(null);
$entityDto->setInstance($dto);
EntityDtoInstanceSetter::setInstance($entityDto, $dto);

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

$this->updateEntityFromDto($entityInstance, $dto);
$entityDto->setInstance(null);
$entityDto->setInstance($entityInstance);
EntityDtoInstanceSetter::setInstance($entityDto, $entityInstance);

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

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

/** @var TDto $entityInstance */
$entityInstance = $newForm->getData();
$context->getEntity()->setInstance(null);
$context->getEntity()->setInstance($entityInstance);
EntityDtoInstanceSetter::setInstance($context->getEntity(), $entityInstance);

if ($newForm->isSubmitted() && $newForm->isValid()) {
$this->processUploadedFiles($newForm);
Expand Down
20 changes: 20 additions & 0 deletions src/Dto/EntityDtoInstanceSetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Protung\EasyAdminPlusBundle\Dto;

use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use ReflectionProperty;

final readonly class EntityDtoInstanceSetter
{
public static function setInstance(EntityDto $entityDto, object|null $instance): void
{
$instanceProperty = new ReflectionProperty($entityDto, 'instance');
$instanceProperty->setValue($entityDto, $instance);

$primaryKeyProperty = new ReflectionProperty($entityDto, 'primaryKeyValue');
$primaryKeyProperty->setValue($entityDto, null);
}
}