0

I am registering Doctrine in Silex in this way:

    // Doctrine
    $this->register(new DoctrineServiceProvider(), array(
        'db.options' => $this['config']['doctrine']['db.options']
    ));
    $this->register(new DoctrineOrmServiceProvider());
    $this['orm.em.options'] = $this['config']['doctrine']['orm.em.options'];

If I insert a duplicated row then I get this exception:

Integrity constraint violation: 1062 Duplicate entry

I have catched this exception using try/catch. If later I try to use again Doctrine, now always this exception is shown:

The EntityManager is closed.

If I try reload the EntityManager following these steps (The EntityManager is closed) :

    if (!$app['orm.em']->isOpen()) {
        $app['orm.em'] = $app['orm.em']->create(
                $app['orm.em']->getConnection(), $app['orm.em']->getConfiguration(), $app['orm.em']->getEventManager()
        );
    }

But now this exception is shown:

Cannot override frozen service "orm.em"

How can I use Doctrine provider in Silex after a Doctrine exception is happened?

Thanks.

Community
  • 1
  • 1
cybtow
  • 157
  • 14
  • You can't. The manager is closed by design after an exception. If you really need to access the database after this sort of exception then create two entity managers and use one solely for cleaning up after an exception. Otherwise, redirect and move on. – Cerad Nov 03 '16 at 21:30
  • Possible duplicate of [The EntityManager is closed](http://stackoverflow.com/questions/14258591/the-entitymanager-is-closed) – olibiaz Nov 04 '16 at 03:06
  • why dont you check if the record exist and then only insert data if there is no existing record. – sonam Nov 04 '16 at 19:46
  • Yes, but this is only an example. Really I need some "mechanism" in order when Doctrine throws an exception, if later I try to use again the DB I can do that. – cybtow Nov 05 '16 at 10:35
  • @Cerad, It is possible unregister my current doctrine Provider/Entity Manager and register a new one? It's importat that my script can run any SQL after Doctrine exceptions, because my PHP script is run un a loop as a "queue worker". Thanks – cybtow Nov 05 '16 at 10:37
  • You can try the Doctrine Registry:resetManager($name) to get a completely new entity manager. From what I have seen it seldom works as needed. The Doctrine ORM is just not designed for long running processes. Probably end up stopping the queue worker then starting a new one. – Cerad Nov 05 '16 at 16:03

1 Answers1

0

It is possible to remove from frozen state. And then create new EntityManager.

$manager = $app['orm.em'];
$app->offsetUnset('orm.em');
$app->offsetSet('orm.em', $manager->create($manager->getConnection(), $manager->getConfiguration()));