kipchak / driver-doctrine
A Doctrine driver (dependency) for the Kipchak API Development Kit (ADK)
Package info
github.com/kipchak-dev/driver-doctrine
Type:driver
pkg:composer/kipchak/driver-doctrine
Requires
- doctrine/orm: ^4
- kipchak/core: ^2 || ^3
- kipchak/driver-filecache: ^1
- kipchak/driver-logger: ^1
Requires (Dev)
- phpunit/phpunit: ^12
Suggests
- kipchak/driver-memcached: To cache entity manager metadata in Memcached ('store' => 'memcached'). Without it, use the file store.
Provides
None
Conflicts
None
Replaces
None
- dev-master
- 1.61
- 1.60
- 1.59
- 1.58
- 1.57
- 1.56
- 1.55
- 1.54
- 1.53
- 1.52
- 1.51
- 1.50
- 1.49
- 1.48
- 1.47
- 1.46
- 1.45
- 1.44
- 1.43
- 1.42
- 1.41
- 1.40
- 1.39
- 1.38
- 1.37
- 1.36
- 1.35
- 1.34
- 1.33
- 1.32
- 1.31
- 1.30
- 1.29
- 1.28
- 1.27
- 1.26
- 1.25
- 1.24
- 1.23
- 1.22
- 1.21
- 1.20
- 1.19
- 1.18
- 1.17
- 1.16
- 1.15
- 1.14
- 1.13
- 1.12
- 1.11
- 1.10
- 1.9
- 1.8
- 1.7
- 1.6
- 1.5
- 1.4
- 1.3
- 1.2
- 1.1
- 1.0
This package is auto-updated.
Last update: 2026-11-24 04:02:31 UTC
README
This driver is based on Symfony's Doctrine DBAL and ORM components (v4).
It is used to interact with MySQL, PGSQL and Oracle databases and supports multiple connections.
Composer Package
kipchak/driver-doctrine
Installation: doctrine/orm 4 is not yet a stable release
This driver uses doctrine/orm 4, which Doctrine has not released as a stable version: Packagist offers the
4.0.x-dev branch, and its newest stable release is 3.7. A project that accepts only stable packages, as the starter
does, therefore needs to allow that one package to be a development version. Kipchak CLI 1.2.1 and later do this for you:
vendor/bin/kipchak add doctrine
adds "doctrine/orm": "^4@dev" to your composer.json beside the driver, so that every other package stays stable.
Without the CLI, run:
composer require kipchak/driver-doctrine "doctrine/orm:^4@dev"
The 4.0.x-dev branch moves with every composer update doctrine/orm; see "Maintaining this driver" below for how each
release of this driver is tested against it. ORM 4 also changes some APIs: QueryBuilder::orderBy() takes PHP 8.6's
SortDirection enum (\SortDirection::Ascending, polyfilled by symfony/polyfill-php86), not the string 'ASC'.
When Doctrine releases ORM 4.0.0, change the constraint to "doctrine/orm": "^4": Composer ranks 4.0.x-dev above
4.0.0, so ^4@dev would keep the development branch.
Sample Config File:
This config file should be placed in your Kipchak project's config directory, as in the examples project at https://github.com/kipchak-dev/examples/blob/master/config/kipchak.doctrine.php.
The config file should look like this (also avalable in sample.config.php):
<?php
use function Kipchak\Core\env;
return
[
'dbal' => [
'enabled' => false,
'connections' => [
// https://www.doctrine-project.org/projects/doctrine-dbal/en/current/reference/configuration.html
'primary' => [
'dbname' => 'mydb',
'user' => env('DB_USER', 'api'),
'password' => env('DB_PASSWORD', 'api'),
'host' => env('DB_HOST', 'mysql'),
'port' => env('DB_PORT', 3306),
'driver' => 'pdo_mysql',
'charset' => 'utf8'
]
]
],
'orm' => [
'enabled' => false,
'entity_managers' => [
'primary' => [
// Enables or disables Doctrine metadata caching
// for either performance or convenience during development.
'dev_mode' => filter_var(env('DEBUG', 'false'), FILTER_VALIDATE_BOOLEAN), // On only for DEBUG=true, 1, on or yes
// List of paths where Doctrine will search for metadata.
// Metadata can be either YML/XML files or PHP classes annotated
// with comments or PHP8 attributes (effectively Doctrine's database entities)
'metadata_dirs' => [
realpath(__DIR__ . '/../api/Entities/Doctrine/Primary')
],
'metadata_format' => 'attributes', // attributes or annotations. Annotations are no longer supported > 1.0.
'connection' => 'primary', // Name of connection from 'dbal' above to use for the ORM
'cache' => [
'enabled' => true, // Kipchak will only look at this if dev_mode = false
'store' => 'memcached', // file or memcached.
],
'cache_config' => [
'memcached' => [
// Pool where Doctrine will cache the processed metadata when 'dev_mode' is false
'pool' => 'cache'
],
'file' => [
// Path where Doctrine will cache the processed metadata when 'dev_mode' is false
'dir' => '/tmp',
]
],
// This will make sure Doctrine ORM does not manage tables beginning with mamluk_
'schema_filter_expression' => '"~^(?!mamluk_)~"',
// Optional: register custom DQL functions on this entity manager.
// See "Custom DQL Functions" below for details.
// 'custom_dql_functions' => [
// 'numeric' => [
// 'JSON_LENGTH' => \DoctrineExtensions\Query\Mysql\JsonLength::class,
// ],
// ],
],
]
],
// migrations:up. Optional: these are the defaults. See "Migrations CLI" below.
'migrations' => [
'lock_timeout' => 300, // seconds to wait for the migration lock
'strict' => false, // true: an "already exists" error fails the migration
],
];
How to use it?
Install it via composer: composer require kipchak/driver-doctrine.
Example Usage
// Load the HTTP driver. $entityManager = Kipchak\Driver\Doctrine\Doctrine::get('entitymanager.primary'); // entitymanager.primary is a reference to the primary entity manager defined in the config file. // Use the $entityManager client as normal.
You can also use the doctrine cli baked into the starter project at https://1x.ax/mamluk/kipchak/starter. If you have created
your project from the starter project, simply run php bin/doctrine to access the CLI after making sure you have installed the
driver via composer and enabeld it in the config file.
Migrations CLI
By default, migrations are loaded from your project path:
<your-project-root>/migrations/database
This path is resolved from the consuming project (the app that installs this package), not from vendor/kipchak/driver-doctrine.
migrations:up supports --file as:
- A single SQL file path (absolute or relative to current working directory)
- A directory path (runs all
.sqlfiles recursively) - A file or directory relative to
<your-project-root>/migrations/database
migrations:down supports --file as:
- A single SQL file path (absolute or relative to current working directory)
- A file relative to
<your-project-root>/migrations/database
migrations:down --file=<directory> is not supported. If --file is omitted, it reverts the latest applied migration.
Each SQL file holds its up statements, then a line -- Down and the statements that undo them. Migrations are recorded
in the schema_migrations table; on PostgreSQL, SQLite and SQL Server each file runs in one transaction. They work on
SQLite from 1.54: earlier releases recorded each migration with NOW(), which SQLite does not have, and failed.
How a file is split into statements (from 1.58)
Statements are sent one at a time. A ; ends a statement only outside:
- comments:
--(in MySQL, followed by a space),#(MySQL only) and/* ... */; - quoted strings and identifiers:
'...',"..."and`...`(backslash escapes in MySQL strings); - PostgreSQL dollar quotes:
$$ ... $$,$tag$ ... $tag$; - the
BEGIN ... ENDbody of aCREATE TRIGGER,PROCEDURE,FUNCTIONorEVENT.
Each statement reaches the database exactly as written, comments included; a piece made of comments alone is not sent.
Before 1.58 every ; ended a statement, so text after a ; in a comment ran as SQL: -- note; DROP TABLE keepme
dropped the table. A file the splitter cannot read cleanly (a quote, comment or block left open) is split at every ;
as before, and a warning names it.
The migration lock (from 1.57)
migrations:up holds a lock for its whole run, so that two runs against one database, such as two pods starting at
once, cannot both find a migration unapplied and both execute it. The lock is taken before schema_migrations is
created or read and released when the run ends, whether it succeeded, failed or threw. The second run waits, then
finds the migrations applied and skips them. The log says so:
Taking the migration lock kipchak.migrations.shop.
Waiting up to 300 s for the migration lock kipchak.migrations.shop, which another run holds.
Obtained the migration lock kipchak.migrations.shop after 2.0 s.
Skipping 2026_01_01_000000_shelves.sql (already applied)
Released the migration lock kipchak.migrations.shop.
| Platform | Lock |
|---|---|
| MySQL, MariaDB | GET_LOCK / RELEASE_LOCK, named kipchak.migrations.<database> |
| PostgreSQL | pg_advisory_lock / pg_advisory_unlock, on a bigint key derived from kipchak.migrations.<database>; the wait is bounded with lock_timeout for that one statement |
| SQLite | None. SQLite allows one writer at a time and runs each migration in a transaction. |
| SQL Server and others | None. Do not start two runs at once. |
The name includes the database name, so runs against two databases on one server do not wait for each other. The lock
belongs to the command's connection: if the process dies, the database releases it when the connection closes.
migrations:down takes the same lock, with the same timeout and messages.
The lock is a session lock, so it does not hold through a transaction-pooling proxy (PgBouncer in transaction mode, ProxySQL multiplexing). Run migrations on a direct or session-pooled connection.
If the lock is not obtained within migrations.lock_timeout seconds, the command prints
Did not obtain the migration lock ... No migration was run. and exits with an error, having created nothing. A
release that fails (RELEASE_LOCK answers 0 or NULL, or the connection is gone) is printed as a warning and does not
change the exit code.
Settings
Both are optional, under migrations in config/kipchak.doctrine.php:
| Setting | Default | Meaning |
|---|---|---|
migrations.lock_timeout |
300 |
Seconds to wait for the migration lock. 0 tries once without waiting. |
migrations.strict |
false |
Whether an "already exists" error fails the migration (see below). |
"Already exists" errors and strict mode
When a statement fails with "already exists", "Duplicate column name" or "Duplicate key name", and migrations.strict
is off (the default), the statement is skipped, the file is recorded as applied, and a warning names the file and the
statement:
Warning: 2026_01_02_000000_books.sql: skipped a statement that was already applied (... Table 'books' already exists). Statement: CREATE TABLE books (id INT PRIMARY KEY, ...)
From 1.58 it is followed by a fixed line for tools to match, which stays stable; match it rather than the warning, whose wording may change:
kipchak-migrations: skipped-existing 2026_01_02_000000_books.sql
A migration that fails, in strict mode or otherwise, prints Failed <file>: <error>, which also stays stable.
This lets a rerun recover from a migration that failed halfway on MySQL, which cannot roll back schema changes, and lets
a schema created by other means (for example orm:schema-tool:create) be followed by migrations:up.
With migrations.strict on, such an error fails the migration like any other: the file is rolled back where the
platform supports transactional schema changes (PostgreSQL, SQLite, SQL Server), it is not recorded, and the command
exits with an error.
The remaining risk: "already exists" is matched by name only, so a rerun that finds a column or index of the same name with a different definition still records the statement as applied. Strict mode catches that.
On PostgreSQL the skip does not help: the failed statement aborts the file's transaction, so the next statement fails and the file is rolled back, with strict mode on or off.
DBAL middleware
From 1.51, DBAL driver middleware registered with Kipchak\Driver\Doctrine\Library\Middleware::push() applies to
every connection the driver creates, its DBAL connections and those of its entity managers. It is read when each
connection is created, on first use, so it can be registered after the driver is initialised. The OpenTelemetry
driver uses it to trace queries. Nothing changes until something is registered.
use Doctrine\DBAL\Driver\Middleware as DriverMiddleware; use Kipchak\Driver\Doctrine\Library\Middleware; // A DBAL middleware, or a callable that receives the connection's name and returns one. Middleware::push('query-log', fn (string $connection): DriverMiddleware => new QueryLog($connection));
Reopenable Entity Managers (long-running workers)
In long-running worker runtimes (FrankenPHP worker mode, RoadRunner, Swoole, ...)
a single EntityManager serves many requests. Doctrine permanently closes an
EntityManager whenever an exception escapes a unit of work — a failed flush(),
an integrity-constraint violation, a deadlock. A closed EntityManager cannot be
reopened, so every service still holding a reference to it fails with
The EntityManager is closed. on all subsequent requests until the worker
process recycles. Replacing the container entry does not help: services
constructed earlier keep their captured reference.
Opt in per entity manager in the config:
'entity_managers' => [ 'primary' => [ // ... 'reopenable' => true, ], ],
Doctrine::get('entitymanager.primary') then returns a
Kipchak\Driver\Doctrine\ReopenableEntityManager — a
Doctrine\ORM\Decorator\EntityManagerDecorator that delegates every call to an
inner EntityManager it can replace. Because all your services hold the (stable)
decorator, swapping the inner instance heals every holder at once. Call this at
the top of each request in your worker loop:
$em = Doctrine::get('entitymanager.primary'); if ($em instanceof Kipchak\Driver\Doctrine\ReopenableEntityManager) { $em->reopenIfClosed(); }
reopenIfClosed() is a no-op while the inner EntityManager is open. When it has
been closed, the decorator:
- rolls back any transaction the failure left dangling on the connection (or closes the connection if the socket is dead — DBAL lazily reconnects);
- builds a fresh EntityManager over the same connection and configuration (or via a custom factory passed to the constructor);
- carries the enabled query filters and their parameters over to the fresh instance, so e.g. a soft-delete filter does not silently switch off.
The flag is off by default; existing configurations keep receiving a plain
EntityManager. If you enable it, type-hint your services against
Doctrine\ORM\EntityManagerInterface rather than the concrete EntityManager.
Custom DQL Functions
Doctrine ORM ships a minimal DQL grammar — vendor-specific helpers such as REGEXP, MATCH … AGAINST, STR_TO_DATE, GROUP_CONCAT, JSON helpers, and so on must be registered on the EntityManager's Configuration before the EntityManager is constructed (Doctrine locks the configuration once the EM is built).
This driver exposes that registration through an optional per-entity-manager custom_dql_functions block in kipchak.doctrine.php. The block has three families, matching Doctrine's three registration methods:
| Family | Doctrine API | Use for |
|---|---|---|
numeric |
addCustomNumericFunction() |
functions that return a number (BIT_COUNT, DAY, …) |
string |
addCustomStringFunction() |
functions that return a string (GROUP_CONCAT, REGEXP, MATCH_AGAINST, …) |
datetime |
addCustomDatetimeFunction() |
functions that return a date/datetime (STR_TO_DATE, DATE_FORMAT, …) |
Each family maps DQL function name → fully qualified class name. The class must extend Doctrine\ORM\Query\AST\Functions\FunctionNode.
Example: enable a few MySQL helpers via beberlei/doctrineextensions
The most popular ready-made bundle of function nodes is beberlei/doctrineextensions:
composer require beberlei/doctrineextensions
Then, in your kipchak.doctrine.php entity-manager block:
'custom_dql_functions' => [ 'numeric' => [ 'BIT_COUNT' => \DoctrineExtensions\Query\Mysql\BitCount::class, ], 'string' => [ 'GROUP_CONCAT' => \DoctrineExtensions\Query\Mysql\GroupConcat::class, 'MATCH_AGAINST' => \DoctrineExtensions\Query\Mysql\MatchAgainst::class, 'REGEXP' => \DoctrineExtensions\Query\Mysql\Regexp::class, ], 'datetime' => [ 'STR_TO_DATE' => \DoctrineExtensions\Query\Mysql\StrToDate::class, ], ],
You can now use these in DQL:
$em->createQuery( "SELECT a FROM App\Entities\Article a WHERE MATCH_AGAINST(a.title, a.body, 'kipchak') > 0" );
JSON helpers
beberlei/doctrineextensions does not ship MySQL JSON_* function nodes. Two clean options:
-
scienta/doctrine-json-functions— a dedicated JSON-functions package coveringJSON_LENGTH,JSON_EXTRACT,JSON_CONTAINS, etc. Register the classes the same way:'custom_dql_functions' => [ 'numeric' => [ 'JSON_LENGTH' => \Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonLength::class, ], ],
-
Hand-roll your own. A
FunctionNodefor a single-arg MySQL function is ~25 lines:namespace App\Doctrine\DqlFunctions; use Doctrine\ORM\Query\AST\Functions\FunctionNode; use Doctrine\ORM\Query\Parser; use Doctrine\ORM\Query\SqlWalker; use Doctrine\ORM\Query\TokenType; final class JsonLength extends FunctionNode { private $value; public function parse(Parser $parser): void { $parser->match(TokenType::T_IDENTIFIER); $parser->match(TokenType::T_OPEN_PARENTHESIS); $this->value = $parser->StringPrimary(); $parser->match(TokenType::T_CLOSE_PARENTHESIS); } public function getSql(SqlWalker $sqlWalker): string { return 'JSON_LENGTH(' . $this->value->dispatch($sqlWalker) . ')'; } }
Register under the
numericfamily (JSON_LENGTHreturns an integer).
Notes
- The block is fully optional and additive. Omit it entirely (or leave any family empty) and behaviour is identical to previous releases.
- Function names are case-insensitive in DQL but Doctrine stores them upper-cased internally — prefer
JSON_LENGTHoverjson_lengthfor clarity. - Functions are registered per entity manager, so different EMs can advertise different DQL grammars.
- You can mix vendored function nodes (e.g.
beberlei/doctrineextensions,scienta/doctrine-json-functions) with your own — just point at anyFunctionNodesubclass.
Release notes
1.58
Backward compatible: nothing to change in a project, and no default changes.
- Text after a
;in a comment no longer runs as SQL.migrations:upandmigrations:downsplit a file with a statement splitter that understands comments, quotes, PostgreSQL dollar quotes and trigger or routine bodies (see "How a file is split into statements"). Before, every;ended a statement, so-- note; DROP TABLE keepmedropped the table and the migration was reported applied. Strings and routine bodies holding;now work too. - Statements are sent exactly as written. A file without a
;in a comment, string or routine body splits exactly as before; this was checked on 147 migration files from 11 Kipchak applications, with identical statements and, replayed into fresh databases, identical schemas. A piece made only of comments is no longer sent: MySQL and SQLite ignored it and PostgreSQL rejected it. - A file the splitter cannot read cleanly is split as before, with a warning.
- A skipped "already exists" statement also prints
kipchak-migrations: skipped-existing <file>, a line kept stable for tools. Every existing line is unchanged. - "Already exists" is recognised exactly as in 1.57.
Minor, not major: the only behaviour that changes is SQL that 1.57 split in the wrong place, which either failed or ran text from a comment.
1.57
migrations:uptakes a lock for its whole run (MySQL and MariaDBGET_LOCK, PostgreSQLpg_advisory_lock; none on SQLite), so two runs started at once no longer apply the same migration twice or skip each other's statements.migrations:downtakes the same lock. New settingmigrations.lock_timeout, default 300 seconds. See "The migration lock" above.- New setting
migrations.strict, defaultfalse. Off, nothing changes: an "already exists" error skips the statement, and the warning now names the migration file and the statement. On, that error fails the migration. migrations:upandmigrations:downwork on a new PostgreSQL database:schema_migrationswas created with aDATETIMEcolumn, a type PostgreSQL does not have, so the commands failed there before doing anything. They now useTIMESTAMPon PostgreSQL.MigrationsUpCommandandMigrationsDownCommandtake an optional second constructor argument, themigrationssettings; without it they are read fromconfig/kipchak.doctrine.php.bin/doctrineis unchanged.- doctrine/orm: the lock did not move, so there are no upstream changes to record.
Running the tests
composer install vendor/bin/phpunit
The suite runs on SQLite without anything else. The migration lock tests also need MySQL 8.4 and PostgreSQL. Start both in Docker:
docker run -d --name kipchak-test-mysql -e MYSQL_ROOT_PASSWORD=test -p 127.0.0.1:33061:3306 mysql:8.4 docker run -d --name kipchak-test-pgsql -e POSTGRES_PASSWORD=test -p 127.0.0.1:54331:5432 postgres:17-alpine
then name them in the environment:
DOCTRINE_TEST_MYSQL_HOST=127.0.0.1 DOCTRINE_TEST_MYSQL_PORT=33061 DOCTRINE_TEST_MYSQL_USER=root DOCTRINE_TEST_MYSQL_PASSWORD=test \ DOCTRINE_TEST_PGSQL_HOST=127.0.0.1 DOCTRINE_TEST_PGSQL_PORT=54331 DOCTRINE_TEST_PGSQL_USER=postgres DOCTRINE_TEST_PGSQL_PASSWORD=test \ vendor/bin/phpunit
Setting a server's HOST variable requests its tests, which then fail, rather than skip, if the server cannot be
reached. Without it they are skipped. Each test creates its own database and drops it afterwards; the user needs the
right to create databases. Remove the containers afterwards with
docker rm -f kipchak-test-mysql kipchak-test-pgsql.
Maintaining this driver
doctrine/orm 4 is unreleased and its 4.0.x-dev branch moves, so every release of this driver follows two rules:
- It is tested against the exact doctrine/orm 4.0.x-dev commit in
composer.lock. The suite covers entity mapping, the repository, DQL and the QueryBuilder, custom DQL functions, migrations up and down, the migration lock against MySQL and PostgreSQL (see "Running the tests"), and worker-mode EntityManager reset (ReopenableEntityManager). Each test has a negative control that shows it fails without what it checks. - Whenever the lock moves to a newer 4.0.x-dev commit, Doctrine's changes between the two commits are read: its
UPGRADE.md, the changelog and the commit log, for breaking changes. The release note records the commit range and each change that affects the driver, or says that none does.
What is a Kipchak Driver?
Kipchak Drivers are used to connect Kipchak to various data sources or storage systems.
They provide a standardized interface for interacting with different data sources, allowing developers to focus on building their applications rather than dealing with the complexities of each data source.
Drivers were introduced as a part of the Kipchak 2.0 release.
Drivers are basically Container Dependencies injected into Kipchak's Service Container.
How do Kipchak Drivers work?
Some Kipchak drivers are wired into the Service Container via a config file (where required).
If applicable, you will find a sample config file in this repository as well as in the starter project at https://1x.ax/mamluk/kipchak/starter.
Each driver defines an implementation of Kipchak's Driver Interface. Where applicable, the dependency may accept a parameter for a specific instance of the driver specified as a config property, for instance, in the case of multiple database connections or S3 buckets.
So you may access the driver by invoking \Mamluk\Kipchak\Driver\Memcached\Memcached::get('cache'), where
cache is the name of one of the memcached connection pools specified in the config file.