Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Doctrine
  4. How to Use Doctrine DBAL
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

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

How to Use Doctrine DBAL

Edit this page

Warning: You are browsing the documentation for Symfony 4.0, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

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 simply 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. In other words, the DBAL library makes it easy to execute queries and perform other database actions.

Tip

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

First, install the Doctrine bundle:

1
$ composer require doctrine/doctrine-bundle

Then configure the DATABASE_URL environment variable in .env:

1
2
3
4
# .env

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

Further things can be configured in config/packages/doctrine.yaml. For the full DBAL configuration options, or to learn how to configure multiple connections, see Doctrine Configuration Reference (DoctrineBundle).

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

1
2
3
4
5
6
7
8
9
10
11
use Doctrine\DBAL\Driver\Connection;

class UserController extends Controller
{
    public function index(Connection $connection)
    {
        $users = $connection->fetchAll('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.

  • YAML
  • XML
  • PHP
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
        http://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/doctrine
        http://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
11
12
// config/packages/doctrine.php
use App\Type\CustomFirst;
use App\Type\CustomSecond;

$container->loadFromExtension('doctrine', array(
    'dbal' => array(
        'types' => array(
            'custom_first'  => CustomFirst::class,
            'custom_second' => 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 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
  • XML
  • PHP
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
        http://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/doctrine
        http://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
$container->loadFromExtension('doctrine', array(
    'dbal' => array(
       'mapping_types' => array(
          'enum'  => 'string',
       ),
    ),
));
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Check Code Performance in Dev, Test, Staging & Production

Check Code Performance in Dev, Test, Staging & Production

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Rootie, a Symfony contributor

Thanks Rootie for being a Symfony contributor

3 commits • 29 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?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • 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
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia