Closed JodyLognoul closed 7 years ago
Are you using the serializer builder class?
The serializer builder has a method "setobjectconstructor", pass to this method an instance of the doctrine object constructor
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.
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.
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
https://github.com/saxulum/saxulum-doctrine-orm-manager-registry-provider is an example for silex
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
Thank you, I will look at it right away and let you know how it goes..
Should I use Doctrine\Common\Persistence\AbstractManagerRegistry?
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
Finally a simple implementation of ManagerRegistry was enough. Just need to clean this up and refactor a bit.
Thank you for your help!
:)
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
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)!