What are common pitfalls when working with Symfony 2 and Doctrine in PHP?

One common pitfall when working with Symfony 2 and Doctrine in PHP is not properly handling database connections and transactions, which can lead to inconsistent data or performance issues. To solve this, always make sure to properly configure and manage database connections and transactions in your Symfony application.

// Example of properly handling database connections and transactions in Symfony with Doctrine

use Doctrine\ORM\EntityManager;

// Get the entity manager from Symfony container
$entityManager = $this->getDoctrine()->getManager();

// Begin a transaction
$entityManager->beginTransaction();

try {
    // Perform database operations here

    // Commit the transaction if successful
    $entityManager->commit();
} catch (\Exception $e) {
    // Rollback the transaction if an exception occurs
    $entityManager->rollback();
    throw $e;
}