use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\Setup; // Set up Doctrine ORM configuration $config = Setup::createAnnotationMetadataConfiguration( ['/path/to/entity/files'], $isDevMode ); // Create Doctrine EntityManager instance $entityManager = EntityManager::create($dbParams, $config); // Get connection used by ObjectManager $connection = $entityManager->getConnection();
use Doctrine\DBAL\DriverManager; // Set up database connection parameters $dbParams = [ 'dbname' => 'my_database', 'user' => 'my_username', 'password' => 'my_password', 'host' => 'localhost', 'driver' => 'pdo_mysql', ]; // Create Doctrine DBAL Connection instance $connection = DriverManager::getConnection($dbParams); // Get connection used by ObjectManager $objectManager->getConnection();In both examples, getConnection() method is called on the ObjectManager instance to fetch the database connection instance used by the persistence layer. The first example is using Doctrine ORM package library while the second example is using Doctrine DBAL package library.