Zend framework 2 with the doctrine of How to reopen Entitymanager after it has been closed by Exception

In the object repository I want to get an object with the given identifier or create a new one if this identifier is not used yet. My solution is to create a new object with an identifier. If possible, I will return it, if not, I want to download it. It is very bad that this is impossible, because the entitymanager is closed.

class TestRepository extends Repository { // create a new entity or load the existing one public function getEntity($pkey) { $entity = new Entity($pkey); // create a new entity and set its id to $pkey $this->getEntityManager()->persist($language); try { $this->getEntityManager()->flush(); // success if $pkey isn't used } catch (DBALException $e) { // this DBALException is catched correct // fail - load the existing one $entity = $this->find(array($pkey)); // another exception is thrown // The EntityManager is closed. } return $entity; } } 

How can I open the EntityManger app?

+4
source share
1 answer

Why are you trying to save an object with a specific primary key from which you do not know if it still exists?

It would be advisable to do a simple search () first, and if it does not return the result, you will create your object and save it.

Mohamed.

0
source

Source: https://habr.com/ru/post/1488726/


All Articles