0
votes

I've setup a multi database connection on my ZF2 project with Doctrine, one to read from database and another to write.

Everything works good when used separately but when i try to use both on the same entity, like:

find a user:

$user = $this->getReaderObjectManager()->getRepository('Entity\User')->findOneBy(array('username' =>'xpto'));

and then try to change something:

$this->getWriterObjectManager()->persist($user); $user->setBlabla('bla');

and then try to flush it:

$this->getWriterObjectManager()->flush();

nothin' happens. no good.

It seems that readerObjectManager and writerObjectManager have their independent "entity pool", so i cant persist and change one entity from the other manager.

Is there a way to pass an entity from one manager to the other, or reference it, or simple that both managers share the same "entity pool"?

thanks in advance.

1

1 Answers

1
votes

You cannot have managed entities across multiple entity managers: that's unsupported and can lead to unexpected behavior.

If you really need to separate reads from writes, do so at connection level using a master/slave connection or by implementing your own Doctrine\DBAL\Connection object.

Also, reads are usually not handled via an ObjectManager, but by an ObjectRepository (where you can implement custom in-memory strategy to optimize read operations).

Another way of handling this is to detach entities from the "read" ObjectManager and merging them back into the "write" ObjectManager.

Again, this is not suggested, and you should handle it at connection level or with a smarter implementation of a repository that fits your needs.