Skip to content

Instantly share code, notes, and snippets.

@yceruto
Last active June 4, 2025 16:27
Show Gist options
  • Save yceruto/4be95e605934311f066b8171c3503167 to your computer and use it in GitHub Desktop.
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
<?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