Coder Social home page Coder Social logo

goodwix / doctrine-json-odm Goto Github PK

View Code? Open in Web Editor NEW
20.0 4.0 5.0 87 KB

JSON Object-Document Mapping bundle for Symfony and Doctrine

License: MIT License

Shell 0.23% Dockerfile 1.30% PHP 98.47%
orm odm json doctrine symfony symfony-bundle symfony4 postgresql database

doctrine-json-odm's Introduction

Doctrine JSON ODM library

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

Inspired by https://github.com/dunglas/doctrine-json-odm

This is beta version of library. Main differences from Dunglas library:

  • library does not store any metadata in json field;
  • doctrine ODM type uses main Symfony serializer service (so you can easily extend serialization process by adding normalizers/denormalizers globally in dependency injection config);
  • automatic registration of ODM types for Doctrine (using Symfony autowiring and autoconfigure features).

Features

  • Object-Document Mapping with database json types
  • Doctrine 2.5+ support
  • PostgreSQL 9.4+ support
  • Symfony 5+ support (not tested with previous versions)
  • MySQL support not tested

Additional features

  • Automatic registering normalizers for use with Java-like collections from ramsey/collection library

Install

Install with Symfony 4

To install the library, use Composer.

composer require goodwix/doctrine-json-odm

Add lines to config/bundles.php (no automatic configuration is available for beta version).

<?php

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

Create package config file config/packages/doctrine-json-odm.yaml with next content.

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

there src/ODM is the root path for your ODM entities (like src/Entity for Doctrine).

Usage

Basic usage with automatic ODM types registration

Create entity class for ODM type in ODM-specific directory (like src/ODM) and mark it with \Goodwix\DoctrineJsonOdm\Annotation\ODM annotation.

namespace App\ODM;

use Goodwix\DoctrineJsonOdm\Annotation\ODM;

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

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

Create doctrine entity class with field type App\ODM\Document.

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

Now you can easily manage your ORM entity with ODM class field

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

Manual usage with Doctrine and Symfony Serializer component

To manually register Doctrine ODM types use ODMType::registerODMType() method.

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. */ }
    }
);

Examples with Symfony application

You can see example of Symfony 4 application with using ODM library in this directory.

Deal with abstract

As an abstract class could not be created as an instance, each concrete children have to be mapped into the abstract class.

Indeed, the Symfony Serializer must know the real type in this case through a discriminator field to determine the real object behind the stored data. For that, it use Symfony\Component\Serializer\Annotation\DiscriminatorMap. More info is available [here]( Maybe this way : Symfony serializer : discriminator

For example, if we have an abstract and 2 children

<?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
{}

Contribution

You can run composer lint before pushing to repository to ensure that there are no code style errors.

Please remember to add some tests if you change library code.

doctrine-json-odm's People

Contributors

chadyred avatar goodwix-owner avatar nqlad avatar strider2038 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

doctrine-json-odm's Issues

[FAQ] Is their a way to handle "abstract" by storing the type of concret class ?

Hello,

Thanks for your works.

I search a way to type with an abstract, but at deserialization time, it doesn't know the concrete "real" type of the stored object. So it says to me "Impossible to create instance of an abstract", logic, as it is an abstract :D

Is their a way to distinct the real type between each children instance at serialization ?

Maybe this way : Symfony serializer : discriminator

I will make a try and do a test with that, or document that....

See you :)

Update readme: How to register type if I use Doctrine directly

Adopting this code:

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. */ }
    }
);

Error in test with phpunit

Hello,

After installaton I don't know why but at doctrine flush time in test, ODMType trigger serializer => Serializer trigger Api Platform => Api Platform trigger router.

Or even, it seems that multi kernel are booted ? But nothing before with dunglas json_document...

3) MyApp\Apps\Core\Tests\Functional\ItemSearchByContainerTest::testWeCouldFindUserItemInAContainer
Symfony\Component\Config\Exception\LoaderLoadException: Cannot load resource "kernel::loadRoutes". Make sure there is a loader supporting the "service" type.

/var/www/html/apps/core/vendor/symfony/config/Loader/DelegatingLoader.php:37
/var/www/html/apps/core/vendor/symfony/framework-bundle/Routing/DelegatingLoader.php:70
/var/www/html/apps/core/vendor/symfony/framework-bundle/Routing/Router.php:68
/var/www/html/apps/core/vendor/api-platform/core/src/Bridge/Symfony/Routing/Router.php:68
/var/www/html/apps/core/vendor/api-platform/core/src/Bridge/Symfony/Routing/RouteNameResolver.php:48
/var/www/html/apps/core/vendor/api-platform/core/src/Bridge/Symfony/Routing/CachedRouteNameResolver.php:47
/var/www/html/apps/core/vendor/api-platform/core/src/Cache/CachedTrait.php:44
/var/www/html/apps/core/vendor/api-platform/core/src/Bridge/Symfony/Routing/CachedRouteNameResolver.php:48
/var/www/html/apps/core/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php:148
/var/www/html/apps/core/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php:128
/var/www/html/apps/core/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php:149
/var/www/html/apps/core/vendor/symfony/serializer/Serializer.php:161
/var/www/html/apps/core/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:231
/var/www/html/apps/core/vendor/symfony/serializer/Serializer.php:161
/var/www/html/apps/core/vendor/symfony/serializer/Serializer.php:134
/var/www/html/apps/core/vendor/goodwix/doctrine-json-odm/src/Type/ODMType.php:114
/var/www/html/apps/core/vendor/doctrine/dbal/src/Statement.php:109
/var/www/html/apps/core/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php:155
/var/www/html/apps/core/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1120
/var/www/html/apps/core/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:415
/var/www/html/apps/core/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:388
/var/www/html/apps/core/var/cache/test/ContainerSV7NsKi/EntityManager_9a5be93.php:136
/var/www/html/apps/core/tests/Functional/ItemSearchByContainerTest.php:40
1) MyApp\Apps\Core\Tests\Smoke\ApiResourcesPermissionsTest::testOperationLeadCanOnlySeeUnloadingTasksThatMatchTheirZonesOrRelatedPickupTaskZone
**Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The "kernel" service is synthetic, it needs to be set at boot time before it can be used.**

/var/www/html/apps/core/vendor/symfony/dependency-injection/Container.php:232
/var/www/html/apps/core/vendor/symfony/dependency-injection/Container.php:197
/var/www/html/apps/core/var/cache/test/ContainerSV7NsKi/getRouting_Loader_XmlService.php:31
/var/www/html/apps/core/var/cache/test/ContainerSV7NsKi/App_KernelTestDebugContainer.php:629
/var/www/html/apps/core/var/cache/test/ContainerSV7NsKi/getRouting_ResolverService.php:25
/var/www/html/apps/core/var/cache/test/ContainerSV7NsKi/App_KernelTestDebugContainer.php:629
/var/www/html/apps/core/var/cache/test/ContainerSV7NsKi/getRouting_LoaderService.php:25
/var/www/html/apps/core/var/cache/test/ContainerSV7NsKi/App_KernelTestDebugContainer.php:629
/var/www/html/apps/core/vendor/symfony/dependency-injection/Container.php:383
/var/www/html/apps/core/vendor/symfony/dependency-injection/Argument/ServiceLocator.php:40
/var/www/html/apps/core/vendor/symfony/framework-bundle/Routing/Router.php:68
/var/www/html/apps/core/vendor/api-platform/core/src/Bridge/Symfony/Routing/Router.php:68
/var/www/html/apps/core/vendor/api-platform/core/src/Bridge/Symfony/Routing/RouteNameResolver.php:48
/var/www/html/apps/core/vendor/api-platform/core/src/Bridge/Symfony/Routing/CachedRouteNameResolver.php:47
/var/www/html/apps/core/vendor/api-platform/core/src/Cache/CachedTrait.php:44
/var/www/html/apps/core/vendor/api-platform/core/src/Bridge/Symfony/Routing/CachedRouteNameResolver.php:48
/var/www/html/apps/core/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php:148
/var/www/html/apps/core/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php:128
/var/www/html/apps/core/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php:149
/var/www/html/apps/core/vendor/symfony/serializer/Serializer.php:161
/var/www/html/apps/core/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:231
/var/www/html/apps/core/vendor/symfony/serializer/Serializer.php:161
/var/www/html/apps/core/vendor/symfony/serializer/Serializer.php:134
/var/www/html/apps/core/vendor/goodwix/doctrine-json-odm/src/Type/ODMType.php:114
/var/www/html/apps/core/vendor/doctrine/dbal/src/Statement.php:109
/var/www/html/apps/core/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php:155
/var/www/html/apps/core/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1120
/var/www/html/apps/core/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:415
/var/www/html/apps/core/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:388
/var/www/html/apps/core/var/cache/test/ContainerSV7NsKi/EntityManager_9a5be93.php:136
/var/www/html/apps/core/tests/Smoke/ApiResourcesPermissionsTest.php:101

Cached ODM annotation options

Auto registrar uses Doctrine Annotation reader while registering odm types. For performance optimization it is usesful to cache ODM annotation options.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.