Fastest way to connect to DB with existing .env
Posted: 10 Jul 2024, 14:34pm - Wednesday

 Several projects now I ended up to create a standalone script to do specific task. Loading doctrine would be using lots of resources. I always want to create a lite-weight script to do specific tasks but connecting to the DB is a bit tricky when you are in a framework (symfony + doctrine).

 
$connection = $_ENV['DATABASE_URL'];
list(
    $driver,,, $username, $password,
    $host, $port, $db_name, $extra1
) = preg_split('/(:|\/|@|\?|&)/', $connection);

$sql = new xSQL([
    'driver' => $driver,
    'port' => $port,
    'database' => $db_name,
    'host' => $host,
    'username' => $username,
    'password' => $password
], true);

 

This is how I do it.. Helps a lot doing this way.