Contributte Doctrine-dbal
Doctrine/DBAL (opens new window) for Nette Framework.
# Content
# Setup
Install package
composer require nettrine/dbal
Register extension
extensions:
  nettrine.dbal: Nettrine\DBAL\DI\DbalExtension
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 (opens new window) integration.
composer require nettrine/cache
extensions:
  nettrine.cache: Nettrine\Cache\DI\CacheExtension
2
Doctrine DBAL (opens new window) needs Doctrine Cache (opens new window) 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 (opens new window) integration.
composer require contributte/console
extensions:
  console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)
  nettrine.dbal: Nettrine\DBAL\DI\DbalExtension
  nettrine.dbal.console: Nettrine\DBAL\DI\DbalConsoleExtension(%consoleMode%)
2
3
4
5
Since this moment when you type bin/console, there'll be registered commands from Doctrine DBAL.

# Configuration
Schema definition
nettrine.dbal:
 debug:
   panel: <boolean>
   sourcePaths: <string[]>
 configuration:
   middlewares: <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: []
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 look like this:
nettrine.dbal:
  debug:
    panel: %debugMode%
    sourcePaths: [%appDir%]
  connection:
    host: localhost
    driver: mysqli
    dbname: nettrine
    user: root
    password: root
2
3
4
5
6
7
8
9
10
Take a look at real Nettrine DBAL configuration example at contributte/webapp-project (opens new window).
# Types
Here is an example of how to register custom type for UUID (opens new window).
dbal:
  connection:
    types:
      uuid: Ramsey\Uuid\Doctrine\UuidType
      uuid_binary_ordered_time:
        class: Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType
        commented: false
    typesMapping:
      uuid_binary_ordered_time: binary
2
3
4
5
6
7
8
9
10
11
For more information about custom types, take a look at the official documention.
- http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html (opens new window)
- http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/custom-mapping-types.html (opens new window)
# Debug
Enable or disable Tracy panel via debug.panel key.
Alternatively, specify your application root path under the debug.sourcePaths key to display correct queries source map in Tracy panel.
# Middlewares
Since Doctrine v3.6 you have to use middlewares instead of event system, see issue doctrine/dbal#5784 (opens new window).
Middlewares are the way how to extend doctrine library or hook to special events.
dbal:
  connection:
    middlewares:
      - MyMiddleware
2
3
4
<?php
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Middleware;
use Tests\Fixtures\Driver\TestDriver;
final class MyMiddleware implements Middleware
{
	public function wrap(Driver $driver): Driver
	{
		return new MyDriver($driver);
	}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
final class MyDriver extends AbstractDriverMiddleware
{
	public function connect(array $params): Connection
	{
		return new MyConnection(parent::connect($params));
	}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
# Logging
Since Doctrine v3.6 you have to use middlewares instead of event system, see issue doctrine/dbal#5784 (opens new window).
To log all queries you should define your own middleware or you can use Doctrine\DBAL\Logging\Middleware.
dbal:
  configuration:
    middlewares:
      logger: Doctrine\DBAL\Logging\Middleware(MyLogger())
2
3
4
You can try our prepared loggers.
dbal:
  configuration:
    middlewares:
      # Write logs to file
      logger: Doctrine\DBAL\Logging\Middleware(
        Nettrine\DBAL\Logger\FileLogger(%tempDir%/db.sql)
      )
      # Show logs in tracy file
      logger: Doctrine\DBAL\Logging\Middleware(
        Nettrine\DBAL\Logger\TracyLogger
      )
2
3
4
5
6
7
8
9
10
11
12
# Examples
# 1. Manual example
composer require nettrine/annotations nettrine/cache nettrine/migrations nettrine/fixtures nettrine/dbal nettrine/orm
# Extension > Nettrine
# => order is crucial
#
extensions:
  # Common
  nettrine.annotations: Nettrine\Annotations\DI\AnnotationsExtension
  nettrine.cache: Nettrine\Cache\DI\CacheExtension
  nettrine.migrations: Nettrine\Migrations\DI\MigrationsExtension
  nettrine.fixtures: Nettrine\Fixtures\DI\FixturesExtension
  # DBAL
  nettrine.dbal: Nettrine\DBAL\DI\DbalExtension
  nettrine.dbal.console: Nettrine\DBAL\DI\DbalConsoleExtension
  # ORM
  nettrine.orm: Nettrine\ORM\DI\OrmExtension
  nettrine.orm.cache: Nettrine\ORM\DI\OrmCacheExtension
  nettrine.orm.console: Nettrine\ORM\DI\OrmConsoleExtension
  nettrine.orm.annotations: Nettrine\ORM\DI\OrmAnnotationsExtension
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 2. Example projects
We've made a few skeletons with preconfigured Nettrine nad Contributte packages.
- https://github.com/contributte/webapp-skeleton (opens new window)
- https://github.com/contributte/apitte-skeleton (opens new window)
# 3. Example playground
- https://github.com/contributte/playground (opens new window) (playground)
- https://contributte.org/examples.html (opens new window) (more examples)
# Other
This repository is inspired by these packages.
- https://github.com/doctrine (opens new window)
- https://gitlab.com/Kdyby/Doctrine (opens new window)
- https://gitlab.com/etten/doctrine (opens new window)
- https://github.com/DTForce/nette-doctrine (opens new window)
- https://github.com/portiny/doctrine (opens new window)
Thank you guys.