goodwix/doctrine-json-odm

适用于 Symfony 和 Doctrine 的 JSON 对象-文档映射包

v0.6.0 2022-01-06 12:42 UTC

This package is auto-updated.

Last update: 2024-08-29 05:00:18 UTC


README

Latest Stable Version Total Downloads License Build Status Code Coverage Scrutinizer Code Quality

受此项目启发

这是库的beta版本。与Dunglas库的主要区别

  • 库不存储任何元数据在JSON字段中;
  • Doctrine ODM类型使用主要的Symfony序列化服务(因此您可以通过在依赖注入配置中全局添加normalizers/denormalizers来轻松扩展序列化过程);
  • 自动注册Doctrine ODM类型(使用Symfony autowiring和autoconfigure特性)。

特性

  • 与数据库json类型的对象-文档映射
  • 支持Doctrine 2.5+
  • 支持PostgreSQL 9.4+
  • 支持Symfony 5+(未测试与旧版本兼容性)
  • 未测试MySQL支持

附加特性

  • 自动注册用于与来自ramsey/collection库的Java-like集合一起使用的normalizers

安装

使用Symfony 4安装

要安装此库,请使用 Composer

composer require goodwix/doctrine-json-odm

将行添加到 config/bundles.php(beta版本没有自动配置)。

<?php

return [
    // ...
    Goodwix\DoctrineJsonOdm\Bridge\Symfony\DoctrineJsonOdmBundle::class => ['all' => true],
];

创建包含以下内容的package配置文件 config/packages/doctrine-json-odm.yaml

doctrine_json_odm:
  mapping:
    paths:
      - '%kernel.project_dir%/src/ODM'

其中 src/ODM 是您的ODM实体的根路径(类似于Doctrine的src/Entity)。

用法

使用自动ODM类型注册的基本用法

在ODM特定目录(如src/ODM)中创建ODM类型的实体类,并用\Goodwix\DoctrineJsonOdm\Annotation\ODM注解标记。

namespace App\ODM;

use Goodwix\DoctrineJsonOdm\Annotation\ODM;

/**
 * @ODM()
 */
class Document
{
    /** @var string */
    public $title;

    /** @var string */
    public $description;
}

创建具有字段类型App\ODM\Document的doctrine实体类。

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\ODM\Document;

/**
 * @ORM\Entity()
 */
class DocumentStorage
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @var int
     */
    public $id;

    /**
     * @ORM\Column(type=Document::class, nullable=true)
     *
     * @var Document
     */
    public $document;
}

现在您可以轻松地使用ODM类字段管理ORM实体

$documentStorage = $entityManager->find(DocumentStorage::class, $id);
$documentStorage->document->title = 'ODM document title';
$documentStorage->document->description = 'ODM document description';

使用Doctrine和Symfony序列化组件的手动使用

要手动注册Doctrine ODM类型,请使用ODMType::registerODMType()方法。

require_once __DIR__.'/../vendor/autoload.php';

use Goodwix\DoctrineJsonOdm\Type\ODMType;
use Symfony\Component\Serializer\SerializerInterface;

class Document { }

ODMType::registerODMType(
    Document::class,
    new class implements SerializerInterface
    {
        public function serialize($data, $format, array $context = [])  { /* Implement serialize() method. */ }
        public function deserialize($data, $type, $format, array $context = [])  { /* Implement deserialize() method. */ }
    }
);

与Symfony应用程序的示例

您可以在此目录中查看使用ODM库的Symfony 4应用程序的示例。

处理抽象

由于无法将抽象类作为实例创建,因此每个具体的子类都必须映射到抽象类。

实际上,在此情况下,Symfony Serializer必须通过一个区分器字段知道实际类型,以确定存储的数据背后的真实对象。为此,它使用Symfony\Component\Serializer\Annotation\DiscriminatorMap。更多信息请参阅[这里]( Maybe this way : Symfony serializer : discriminator

例如,如果我们有一个抽象类和2个子类

<?php

declare(strict_types=1);

namespace App\Whatever;

use Symfony\Component\Serializer\Annotation\DiscriminatorMap;

#### PHP8 Attribute ####
#[DiscriminatorMap(
    typeProperty: 'type',
    mapping: [
        'myChildTypeName' => 'App\Whatever\Child',
        'myChild2TypeName' => 'App\Whatever\Child2',
    ]
)]
####> PHP8 Attribute ####


#### PHP < PHP8 ####
/**
 * @DiscriminatorMap(typeProperty="type", mapping={
 *    "myChildTypeName"="App\Whatever\Child",
 *    "myChild2TypeName"="App\Whatever\Child2"
 * })
 */
####> PHP < PHP8 ####


abstract class MyAbstract
{
}

class Child extends MyAbstract
{}


class Child2 extends MyAbstract
{}

贡献

在向仓库推送之前,您可以使用composer lint运行以确保没有代码风格错误。

请记住,如果您更改库代码,请添加一些测试。