定制 edudobay/doctrine-symfony-serializer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

edudobay/doctrine-symfony-serializer

最新稳定版本:v0.4.0

Composer 安装命令:

composer require edudobay/doctrine-symfony-serializer

包简介

Map fields in Doctrine ORM using Symfony Serializer

README 文档

README

Version License

This is a proof of concept for a mapping fields with Doctrine ORM so they can be serialized with the Symfony Serializer component, without the need to create a mapping type for each possible data type.

Sometimes you just need to store a complex data type (generally a Value Object) in a JSON field and not worry about database schemas, extra columns or tables, Doctrine embeddables, custom mappings.

Installation

This library requires PHP 8.0 or later.

composer require edudobay/doctrine-symfony-serializer

Usage

See the examples directory for a working code example.

Abridged example

Add this to your application setup:

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);

In your entities, have your domain object as you like, and introduce a private backing field that will make it persistent:

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;
}

统计信息

  • 总下载量: 7.21k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-12-16