Doctrine Symfony2 – EntityManager Closed – Always enclose flush inside a try catch block and log exceptions

Hi,

This is not a tutorial but a good practice advice, about flush of objects in Doctrine with Symfony2.
A few days ago, we have encountered a strange error: “Entity Manager Closed”.

After several minutes of research, we found the problem.

We were trying to save a NULL value inside a NOT NULL field in our database, making the flush of objects failing.
When flush fails, it creates an exception like this:

 Doctrine\ORM\Exception "The EntityManager is closed".

When this error occurs, then entityManager is voluntarily closed, and following “flush” will not work.

The good practice is to systematically enclose all flush, inside a try catch block, and log the exception message and trace.
Let’s see an example.

    try {
        $em->flush();
    } catch (\Exception $e) {
        $msg = '### Message ### \n'.$e->getMessage().'\n### Trace ### \n'.$e->getTraceAsString();
        $this->container->get('logger')->critical($msg);
        // Here put you logic now you now that the flush has failed and all subsequent flush will fail as well
    }

This is a good practice that you’ll have to use in your further codes.

Advertisements
Doctrine Symfony2 – EntityManager Closed – Always enclose flush inside a try catch block and log exceptions

Php-Mysql-Api a powerful plug-n-play Php api for Mysql database tested with codeception

Hi everyone,

This is my last php project and I want to share it with all of you.
It is a simple but powerful object that gives you a php api for mysql database.

With php-mysql-api, you will be able in 60 seconds chrono, to:
– Access your database datas with a RESTful syntax
– Limit access to specific ips
– POST, GET, PUT and DELETE datas.

It is fully tested with CodeCeption.

The repository is here: https://github.com/edouardkombo/php-mysql-api

I really hope it will save you a lot of time.

Php-Mysql-Api a powerful plug-n-play Php api for Mysql database tested with codeception

Cannot delete or update a parent row: a foreign key constraint fails

If you have encountered this error, this is maybe because:

  1. There is a table within another schema (“database” in mysql terminology) which has a FK reference
  2. The innodb internal data dictionary is out of sync with the mysql one.

To apply your queries, simply bypass foreign keys security check.
This is the solution:

SET FOREIGN_KEY_CHECKS=0;
YOUR_QUERY here

After your queries have been done, reset to default foreign keys security check:

SET FOREIGN_KEY_CHECKS=1;

You’re done.

Cannot delete or update a parent row: a foreign key constraint fails

Symfony2 differences between doctrine:schema:update and doctrine:migrations

When you have to create or update your database schema from entities, you are used to type these commands:

php app/console doctrine:schema:update --dump-sql
php app/console doctrine:schema:update --force

Doctrine offers another extension called Migrations that helps you:
– Save your different database schemas during the life of your project
– Move tables and databases with their content, what a simple doctrine:schema:update can’t do.
– Generate accessible migrations queries from the difference between your entities and your database schema.
– Include your own migration queries.

To use doctrine migrations, you will often use these commands:

php app/console doctrine:migrations:diff #Generate migrations queries
php app/console doctrine:migrations:migrate #Refresh your database schema from migrations queries

Finally, for rapid schema updates, use doctrine:schema:update, but, for complex modifications in your database, prefer doctrine:migrations.

Symfony2 differences between doctrine:schema:update and doctrine:migrations