Last active
June 4, 2025 16:27
-
-
Save yceruto/4be95e605934311f066b8171c3503167 to your computer and use it in GitHub Desktop.
Doctrine data storage sample for FormFlow https://github.com/symfony/symfony/pull/60212
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Form\Data\Storage; | |
use Doctrine\ORM\EntityManagerInterface; | |
use Symfony\Component\Form\Flow\DataStorage\DataStorageInterface; | |
class DoctrineDataStorage implements DataStorageInterface | |
{ | |
public function __construct( | |
private readonly EntityManagerInterface $em, | |
) { | |
} | |
public function save(object|array $data): void | |
{ | |
$this->em->persist($data); // it does nothing if the entity is already persisted | |
$this->em->flush(); | |
} | |
public function load(object|array|null $default = null): object|array|null | |
{ | |
if (!\is_object($default) || !$this->em->contains($default)) { | |
return $default; | |
} | |
$this->em->initializeObject($default); | |
return $default; | |
} | |
public function clear(): void | |
{ | |
$this->em->flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment