I'm creating a PHP package that will reside in the /vendor
directory and it needs to access the database using Doctrine. In my main Symfony 4.2 code, I can access the database by using autowiring in the constructors like this:
public function __construct(EntityManagerInterface $em)
However, this only works with Symfony and does not seem to work inside public bundles. Please note that I need to access the default database connection, I do not want to create a new connection if one already exists.
I am trying to create a package that will work both with and without Symfony, so what I really need is the raw PHP to get the current database connection (not a new connection) without all the autowiring magic.
If the package is running under Symfony, it should use the current Symfony database connection. If not, it should create its own connection.
For example, I can open a new connection like this:
$conn = \Doctrine\DBAL\DriverManager::getConnection($params, $config)
That works fine, but I need a way to get the current Symfony database connection (something like this):
$conn = \Doctrine\DBAL\DriverManager::getCurrentConnection()
Unfortunately, there is no such function and I can't figure out how to create my own version of it.