Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
JodyLognoul opened this issue Apr 6, 2017 · 12 comments
Closed

Comments

@JodyLognoul
Copy link

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
Copy link
Collaborator

goetas commented Apr 6, 2017

Are you using the serializer builder class?

@goetas
Copy link
Collaborator

goetas commented Apr 6, 2017

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

@JodyLognoul
Copy link
Author

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
Copy link
Collaborator

goetas commented Apr 6, 2017

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
Copy link
Collaborator

goetas commented Apr 6, 2017

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 goetas closed this as completed Apr 6, 2017
@goetas
Copy link
Collaborator

goetas commented Apr 6, 2017

@goetas
Copy link
Collaborator

goetas commented Apr 6, 2017

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
Copy link
Author

JodyLognoul commented Apr 7, 2017

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

@JodyLognoul
Copy link
Author

Should I use Doctrine\Common\Persistence\AbstractManagerRegistry?

@goetas
Copy link
Collaborator

goetas commented Apr 7, 2017

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
Copy link
Author

JodyLognoul commented Apr 7, 2017

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

Thank you for your help!

:)

@OlivierTamno
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants