定制 webit/doctrine-jms-json 二次开发

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

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

webit/doctrine-jms-json

Composer 安装命令:

composer require webit/doctrine-jms-json

包简介

Converts complex objects using JMS Serializer

README 文档

README

Persist your Value Objects with Doctrine using JMS Serializer. The DBAL Type supports anything that can be handled by JMS Serializer (scalar, array, Doctrine Collections, DateTime, etc).

Installation

Add "webit/doctrine-jms-json": "^2.0.0" to the require section of your composer.json

{
    "require": {
        "webit/doctrine-jms-json": "^3.0.0"
    }
}

Usage

Configure register new Doctrine DBAL type and configure the Serializer:

# boostrap.php

use Doctrine\DBAL\Types\Type;
use Webit\DoctrineJmsJson\DBAL\JmsJsonType;
use JMS\Serializer\SerializerBuilder;
use Webit\DoctrineJmsJson\Serializer\Type\DefaultSerializerTypeResolver;

Type::addType('jms_json', 'Webit\DoctrineJmsJson\DBAL\JmsJsonType');

$serializer = SerializerBuilder::create()->build();
// initialize JmsJsonType
JmsJsonType::initialize($serializer, new DefaultSerializerTypeResolver());

Now you can use "jms_json" in the Doctrine field mapping as a type.

See full configuration of JMS Serializer here See full documentation of Doctrine Mapping here See Doctrine Custom Types documentation here

Example

"jms_json" DBAL type can be very useful to store Value Objects or objects that structures may vary.

Consider example of a slider on a web page. Let class BannerSet contain the Slider configuration.

interface Slider
{
}

class BannerSet
{
    private $id;
    
    /** Slider */
    private $slider;
    
    /**
     * @return Slider
     */
    public function slider()
    {
        return $this->slider;
    }
    
    /**
     * @param Slider $slider
     */
    public function changeSlider(Slider $slider)
    {
        $this->slider = $slider;
    }
}

There are plenty of different JavaScript slider configurations so we don't know in advance the Slider structure. Let's introduce two implementations:

use JMS\Serializer\Annotation as JMS;

class Slider1 implements Slider
{
    /**
     * @JMS\Type("string")
     */
    private $theme;
    
    /**
     * @JMS\Type("double")
     */
    private $slideTime;
    
    // ...
}

class Slider2 implements Slider
{ 
    /**
     * @JMS\Type("array<string>")
     */
    private $effects;

    /**
     * @JMS\Type("boolean")
     */
    private $stopOnHover;
    
    /**
     * @JMS\Type("integer")
     */
    private $pauseTime;
 
    // ...
}

To persist the BannerSet in the database using Doctrine ORM we need to provide the mapping. Using the standard relational approach we need to map BannerSet and every Slider (probably using [single table inheritance]). It means, we'll have two tables in the database "banner_set" and "slider" with one-to-one relationship between them - quite complex for storing slider configuration. The more, this approach requires to store Slider instances like Entities (we have to introduce ID to store in the table), what breaks our model.

Slider configuration is just object we need to store, but we don't need to query for it. So it's much simpler to store it in a JSON format in one column of "banner_set" table.

BannerSet Doctrine mapping file would look like:

entity:
    class: BannerSet
    id:
        id:
            type: int
    fields:
        slider:
            type: jms_json

And that's it!

Obviously, we need to provide the Serializer mapping for Slider1 and Slider2 (what can be done in different ways, eg. via Annotations - check the JMSSerializer documentation for more details).

Symfony 2/3 integration

See WebitDoctrineJsonBundle Symfony 2/3 integration.

Tests

composer install
./vendor/bin/phpunit

Change log

Version 2.0.0

Introduces support for native JSON type database field. Internally data are stored in a valid JSON format ('{"_jms_type": "Deserialisation Type", "data": "DATA TO BE DESERIALISED"}'). This breaks backwards compatibility with version 1.x. Version 1.x cannot be upgraded to 2.x without manual conversion of the saved data.

Version 2.1.0

Provides backwards compatibility with version 1.x on conversion to PHP Value (reading from the database). Please note, the data read from the format of version 1.x will be converted automatically to the new format during flush process of the entity.

Version 3.0.0

Provides support for higher JMS Serializer versions (2.x, 3.x). This version required PHP >= 7.2.

webit/doctrine-jms-json 适用场景与选型建议

webit/doctrine-jms-json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 54.95k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2016 年 08 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 webit/doctrine-jms-json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 webit/doctrine-jms-json 我们能提供哪些服务?
定制开发 / 二次开发

基于 webit/doctrine-jms-json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-28