# Nettrine DBAL

Doctrine/DBAL for Nette Framework.

# Content

# Setup

Install package

composer require nettrine/dbal
1

Register extension

extensions:
  nettrine.dbal: Nettrine\DBAL\DI\DbalExtension
1
2

# Relying

Take advantage of enpowering this package with 2 extra packages:

  • doctrine/cache
  • symfony/console

# doctrine/cache

This package relies on doctrine/cache, use prepared nettrine/cache integration.

composer require nettrine/cache
1
extensions:
  nettrine.cache: Nettrine\Cache\DI\CacheExtension
1
2

Doctrine DBAL needs Doctrine Cache to be configured. If you register nettrine/cache extension it will detect it automatically.

# symfony/console

This package relies on symfony/console, use prepared contributte/console integration.

composer require contributte/console
1
extensions:
  console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)

  nettrine.dbal: Nettrine\DBAL\DI\DbalExtension
  nettrine.dbal.console: Nettrine\DBAL\DI\DbalConsoleExtension(%consoleMode%)
1
2
3
4
5

Since this moment when you type bin/console, there'll be registered commands from Doctrine DBAL.

Console Commands

# Configuration

Schema definition

nettrine.dbal:
 debug:
   panel: <boolean>
   sourcePaths: <string[]>
 configuration:
   sqlLogger: <service>
   resultCache: <service>
   filterSchemaAssetsExpression: <string>
   autoCommit: <boolean>

 connection:
   url: <string>
   pdo: <string>
   memory: <string>
   driver: <string>
   driverClass: <string>
   host: <string>
   dbname: <string>
   servicename: <string>
   user: <string>
   password: <string>
   charset: <string>
   portability: <int>
   fetchCase: <int>
   persistent: <boolean>
   wrapperClass: <class>
   types: []
   typesMapping: []
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

Under the hood

Minimal configuration could looks like this:

nettrine.dbal:
  debug:
    panel: %debugMode%
    sourcePaths: [%appDir%]
  connection:
    host: localhost
    driver: mysqli
    dbname: nettrine
    user: root
    password: root
1
2
3
4
5
6
7
8
9
10

Take a look at real Nettrine DBAL configuration example at Nutella Project.

# Types

Here is example how to register custom type for UUID.

dbal:
  connection:
    types:
      uuid_binary_ordered_time:
        class: Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType
        commented: false

      typesMapping:
        uuid_binary_ordered_time: binary
1
2
3
4
5
6
7
8
9

For more information about custom types, follow the official documention.

  • http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html
  • http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/custom-mapping-types.html

# Debug

Enable or disable Tracy panel via debug.panel key.

Alternatively specify your application root path under debug.sourcePaths key to display correct queries source map in Tracy panel.

# Events

You can use native Doctrine DBAL event system.

Create your subscriber class which implements EventSubscriber interface. Dependency injection with autowiring is enabled.

namespace App;

use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;

final class PostConnectSubscriber implements EventSubscriber
{
  public function postConnect(ConnectionEventArgs $args): void
  {
    // Magic goes here...
  }

  public function getSubscribedEvents(): array
  {
    return [Events::postConnect];
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Register your subscriber as a service in NEON file.

services:
  subscriber1:
    class: App\PostConnectSubscriber
1
2
3

# Bridges

# PSR-3

To log all queries with a PSR-3 logger, define service under configuration.sqlLogger key. Monolog provides PSR compatible services.

dbal:
  configuration:
    sqlLogger: Nettrine\DBAL\Logger\PsrLogger()
1
2
3

# Examples

You can find more examples in planette playground repository.