Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Doctrine
  4. How to Use Doctrine DBAL

How to Use Doctrine DBAL

Edit this page

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
2
3
4
# .env (or override DATABASE_URL in .env.local to avoid committing your changes)

# customize this line!
DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=8.0.37"

Further things can be configured in config/packages/doctrine.yaml - see Doctrine Configuration Reference (DoctrineBundle). 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// src/Controller/UserController.php
namespace App\Controller;

use Doctrine\DBAL\Connection;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class UserController extends AbstractController
{
    public function index(Connection $connection): Response
    {
        $users = $connection->fetchAllAssociative('SELECT * FROM users');

        // ...
    }
}

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.

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

    <doctrine:config>
        <doctrine:dbal>
            <doctrine:type name="custom_first" class="App\Type\CustomFirst"/>
            <doctrine:type name="custom_second" class="App\Type\CustomSecond"/>
        </doctrine:dbal>
    </doctrine:config>
</container>
1
2
3
4
5
6
7
8
9
10
// config/packages/doctrine.php
use App\Type\CustomFirst;
use App\Type\CustomSecond;
use Symfony\Config\DoctrineConfig;

return static function (DoctrineConfig $doctrine): void {
    $dbal = $doctrine->dbal();
    $dbal->type('custom_first')->class(CustomFirst::class);
    $dbal->type('custom_second')->class(CustomSecond::class);
};

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 type. 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:

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

    <doctrine:config>
        <doctrine:dbal>
            <doctrine:mapping-type name="enum">string</doctrine:mapping-type>
        </doctrine:dbal>
    </doctrine:config>
</container>
1
2
3
4
5
6
7
8
// config/packages/doctrine.php
use Symfony\Config\DoctrineConfig;

return static function (DoctrineConfig $doctrine): void {
    $dbalDefault = $doctrine->dbal()
        ->connection('default');
    $dbalDefault->mappingType('enum', 'string');
};
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 7.1 is backed by

    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    No stress: we've got you covered with our 116 automated quality checks of your code

    No stress: we've got you covered with our 116 automated quality checks of your code

    Version:

    Table of Contents

    • Registering custom Mapping Types
    • Registering custom Mapping Types in the SchemaTool

    Symfony footer

    Avatar of Oriol Viñals, a Symfony contributor

    Thanks Oriol Viñals for being a Symfony contributor

    2 commits • 16 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony