schmittjoh / serializer

Library for (de-)serializing data of any complexity (supports JSON, and XML)
http://jmsyst.com/libs/serializer
MIT License
2.32k stars 587 forks source link

DoctrineObjectConstructor: how to use it without Symfony, in a PHP project #741

Closed JodyLognoul closed 7 years ago

JodyLognoul commented 7 years ago

Hello JMS,

In order to attach a deserialized existing entity to doctrine entity manager, I would like to use the DoctrineObjectConstructor (instantiate it, and give it to the SerializerBuilder). How can I do it? Do I have to give an implementation to ManagerRegistry interface?

Thank you ! (And thank you for the lib)!

goetas commented 7 years ago

Are you using the serializer builder class?

goetas commented 7 years ago

The serializer builder has a method "setobjectconstructor", pass to this method an instance of the doctrine object constructor

JodyLognoul commented 7 years ago

Thank you for your fast response.

Yes, that's what I am trying to do, but I can't find a way to instantiate it (DoctrineObjectConstructor so). To be exact, I don't know what to give to the first param $managerRegistry:

public function __construct(ManagerRegistry $managerRegistry, ObjectConstructorInterface $fallbackConstructor)

Thank you.

goetas commented 7 years ago

The interface is relatively simple, you have to provide an implementation depending on the framework you are using.

If you are using symfony, zend, Silex or laravel, there are various implementation available. If you are not using any of them or a custom framework, then you have to provide your own implementation of the interface.

goetas commented 7 years ago

The implementation should be trivial, since is just a registry of doctrine entity managers and connections. If you have one single entity manager and you are not aiming for lazy loading, then should be even easier

goetas commented 7 years ago

https://github.com/saxulum/saxulum-doctrine-orm-manager-registry-provider is an example for silex

goetas commented 7 years ago

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\EntityManager;

class SimpleManagerRegistry implements ManagerRegistry
{
    /**
     * @var EntityManager
     */
    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function getDefaultConnectionName()
    {
        return 'blah';
    }

    public function getConnection($name = null)
    {
        return $this->entityManager->getConnection();
    }

    public function getConnections()
    {
        return [$this->getDefaultConnectionName() => $this->getConnection()];
    }

    public function getConnectionNames()
    {
        return [$this->getDefaultConnectionName()];
    }

    public function getDefaultManagerName()
    {
        return 'em';
    }

    public function getManager($name = null)
    {
        return $this->entityManager;
    }

    public function getManagers()
    {
        return [$this->getDefaultManagerName() => $this->entityManager];
    }

    public function resetManager($name = null)
    {
        // todo
    }

    public function getAliasNamespace($alias)
    {
        return 'App';
    }

    public function getManagerNames()
    {
        return [$this->getDefaultManagerName()];
    }

    public function getRepository($persistentObject, $persistentManagerName = null)
    {
        return $this->entityManager->getRepository(ClassUtils::getRealClass($persistentObject));
    }

    public function getManagerForClass($class)
    {
        return $this->entityManager->getRepository($class);
    }
}

This can be a skeleton... i suggest you to do something better, but is a starting point

JodyLognoul commented 7 years ago

Thank you, I will look at it right away and let you know how it goes..

JodyLognoul commented 7 years ago

Should I use Doctrine\Common\Persistence\AbstractManagerRegistry?

goetas commented 7 years ago

up to you, using AbstractManagerRegistry is a good practice, but involves some "service" management... is a good idea if you have a dependency injection container (or a service locator) already implemented in your framework

JodyLognoul commented 7 years ago

Finally a simple implementation of ManagerRegistry was enough. Just need to clean this up and refactor a bit.

Thank you for your help!

:)

OlivierTamno commented 1 year ago

Finally a simple implementation of ManagerRegistry was enough. Just need to clean this up and refactor a bit.

Thank you for your help!

:)

Hello JodyLognoul, Please can share your solution there please. Thank in advance