phpsymfonydoctrinephp-8symfony6

Symfony Doctrine: check connection to a database


Since a while checking a connection to a database in a controller was working fine doing the following:

$em->getConnection()->connect();
$connected = $em->getConnection()->isConnected();

But I now have a deprecation:

php.INFO: User Deprecated: Public access to Connection::connect() is deprecated.

How should I test my database connection now ?


Solution

  • There is now a deprecation notice when connection is used outside the library:

    Deprecation::triggerIfCalledFromOutside(
                'doctrine/dbal',
                'https://github.com/doctrine/dbal/issues/4966',
                'Public access to Connection::connect() is deprecated.',
            );
    

    You need to use getNativeConnection() instead (which is from the same Connection class)

    $em->getNativeConnection()->isConnected();
    

    You can read the PR containing those changes.