Symfony Doctrine: check connection to a database

449 views Asked by At

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 ?

1

There are 1 answers

0
Dylan KAS On BEST ANSWER

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.