PHP code example of edudobay / doctrine-symfony-serializer

1. Go to this page and download the library: Download edudobay/doctrine-symfony-serializer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

edudobay / doctrine-symfony-serializer example snippets


use Edudobay\DoctrineSerializable\ReflectionClassMetadataFactory;
use Edudobay\DoctrineSerializable\SerializationHandler;
use Edudobay\DoctrineSerializable\PersistenceEventSubscriber;

$serializer = ...; // Symfony Serializer
$entityManager = ...; // Doctrine ORM EntityManager

$subscriber = new PersistenceEventSubscriber(new SerializationHandler(
    $serializer,
    // You might want to cache this. See Psr6CacheClassMetadataFactory
    new ReflectionClassMetadataFactory()
));

$entityManager->getEventManager()->addEventSubscriber($subscriber);

use Doctrine\ORM\Mapping as ORM;
use Edudobay\DoctrineSerializable\Attributes\Serializable;

#[ORM\Entity]
class User
{
    // Backing field
    #[ORM\Column('address', type: 'json')]
    private array $_address = [];
    // The actual domain object
    #[Serializable]
    public Address $address;

    // For arrays:
    #[ORM\Column('badges', type: 'json')]
    private array $_badges = [];

    #[Serializable]
    /** @var Badge[] */
    public array $badges;

    // OPTIONAL: use arrayItemType instead of docblock array types
    #[Serializable(arrayItemType: Badge::class)]
    public array $badges;
}