How to Use Doctrine DBAL

How to Use Doctrine DBAL

Note

This article is about the Doctrine DBAL. Typically, you’ll work with the higher level Doctrine ORM layer, which uses the DBAL behind the scenes to actually communicate with the database. To read more about the Doctrine ORM, see “Databases and the Doctrine ORM”.

The Doctrine Database Abstraction Layer (DBAL) is an abstraction layer that sits on top of PDO and offers an intuitive and flexible API for communicating with the most popular relational databases. The DBAL library allows you to write queries independently of your ORM models, e.g. for building reports or direct data manipulations.

Tip

Read the official Doctrine DBAL Documentation to learn all the details and capabilities of Doctrine’s DBAL library.

First, install the Doctrine orm Symfony pack:

  1. $ composer require symfony/orm-pack

Then configure the DATABASE_URL environment variable in .env:

  1. # .env (or override DATABASE_URL in .env.local to avoid committing your changes)
  2. # customize this line!
  3. DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"

Further things can be configured in config/packages/doctrine.yaml - see Doctrine DBAL Configuration. Remove the orm key in that file if you don’t want to use the Doctrine ORM.

You can then access the Doctrine DBAL connection by autowiring the Connection object:

  1. // src/Controller/UserController.php
  2. namespace App\Controller;
  3. use Doctrine\DBAL\Driver\Connection;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. class UserController extends AbstractController
  7. {
  8. public function index(Connection $connection): Response
  9. {
  10. $users = $connection->fetchAll('SELECT * FROM users');
  11. // ...
  12. }
  13. }

This will pass you the database_connection service.

Registering custom Mapping Types

You can register custom mapping types through Symfony’s configuration. They will be added to all configured connections. For more information on custom mapping types, read Doctrine’s Custom Mapping Types section of their documentation.

  • YAML

    1. # config/packages/doctrine.yaml
    2. doctrine:
    3. dbal:
    4. types:
    5. custom_first: App\Type\CustomFirst
    6. custom_second: App\Type\CustomSecond
  • XML

    1. <!-- config/packages/doctrine.xml -->
    2. <container xmlns="http://symfony.com/schema/dic/services"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:doctrine="http://symfony.com/schema/dic/doctrine"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd
    7. http://symfony.com/schema/dic/doctrine
    8. https://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
    9. <doctrine:config>
    10. <doctrine:dbal>
    11. <doctrine:type name="custom_first" class="App\Type\CustomFirst"/>
    12. <doctrine:type name="custom_second" class="App\Type\CustomSecond"/>
    13. </doctrine:dbal>
    14. </doctrine:config>
    15. </container>
  • PHP

    1. // config/packages/doctrine.php
    2. use App\Type\CustomFirst;
    3. use App\Type\CustomSecond;
    4. $container->loadFromExtension('doctrine', [
    5. 'dbal' => [
    6. 'types' => [
    7. 'custom_first' => CustomFirst::class,
    8. 'custom_second' => CustomSecond::class,
    9. ],
    10. ],
    11. ]);

Registering custom Mapping Types in the SchemaTool

The SchemaTool is used to inspect the database to compare the schema. To achieve this task, it needs to know which mapping type needs to be used for each database types. Registering new ones can be done through the configuration.

Now, map the ENUM type (not supported by DBAL by default) to the string mapping type:

  • YAML

    1. # config/packages/doctrine.yaml
    2. doctrine:
    3. dbal:
    4. mapping_types:
    5. enum: string
  • XML

    1. <!-- config/packages/doctrine.xml -->
    2. <container xmlns="http://symfony.com/schema/dic/services"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:doctrine="http://symfony.com/schema/dic/doctrine"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd
    7. http://symfony.com/schema/dic/doctrine
    8. https://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
    9. <doctrine:config>
    10. <doctrine:dbal>
    11. <doctrine:mapping-type name="enum">string</doctrine:mapping-type>
    12. </doctrine:dbal>
    13. </doctrine:config>
    14. </container>
  • PHP

    1. // config/packages/doctrine.php
    2. $container->loadFromExtension('doctrine', [
    3. 'dbal' => [
    4. 'mapping_types' => [
    5. 'enum' => 'string',
    6. ],
    7. ],
    8. ]);

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.