Doctrine DBAL Documentation
repository·4.4.x·Indexed 27 days ago
https://github.com/doctrine/dbalA powerful Database Abstraction Layer for PHP providing tools for database schema introspection and management. It offers a unified API to interact with various database systems, abstracting driver nuances through a wrapper and driver layer architecture. Features include schema management, custom database types, result caching via PSR-6, and support for multiple platforms including MySQL, PostgreSQL, SQLite, and SQL Server.
What's inside Doctrine DBAL
- Doctrine DBAL (Database Abstraction Layer) is a powerful library providing features for database schema introspection and schema management. It allows developers to interact with various database systems through a unified API, abstracting away the specific nuances of different database drivers.
Understand the Doctrine DBAL Architecture
4.4.xDoctrine DBAL is structured into two main layers: a wrapper layer and a driver layer.
Wrapper Layer
Applications typically interact directly with these wrapper components, which provide a consistent API regardless of the underlying database:
Doctrine\DBAL\Connection(wraps a driver connection)Doctrine\DBAL\Statement(wraps a driver statement)Doctrine\DBAL\Result(wraps a driver result)
Driver Layer
The driver layer abstracts specific PHP database APIs (like
pdo_mysqlormysqli) using four core interfaces:Doctrine\DBAL\DriverDoctrine\DBAL\Driver\ConnectionDoctrine\DBAL\Driver\StatementDoctrine\DBAL\Driver\Result
A
Doctrine\DBAL\Driverimplementation is responsible for translating connection parameters and acting as a factory for platform, schema manager, and exception converter components.Understand the Doctrine DBAL Type system
4.4.xDoctrine DBAL provides a type translation system that converts between database-specific SQL types and PHP data types. This allows for database-independent applications by handling the conversion to and from PHP values and generating platform-independent SQL for any
Doctrine\\DBAL\\\ Types\\\ Type.Key characteristics:
- Abstraction: Types are abstracted across all supported database vendors.
- Flyweight Pattern: Type instances are flyweights. There is only ever one instance of a specific type, and instances are not allowed to contain any state.
- ORM Usage: If you are using the Doctrine ORM, you generally do not need to interact with the Type system directly unless you are implementing database vendor-specific types that are not included in Doctrine DBAL.
Upgrade to 3.10: Support for PHP 8.4 PDO subclasses
4.4.xIn version 3.10.2, support for new PDO subclasses introduced in PHP 8.4 was backported. When callinggetNativeConnection()on a connection established through a PDO driver on PHP 8.4, you will receive instances of the new PDO subclasses (e.g.,Pdo\MysqlorPdo\Pgsql) instead of the genericPDOclass.Upgrade to 4.2: Handle PDO subclasses on PHP 8.4
4.4.xWhen using PHP 8.4, callinggetNativeConnection()on a connection established through a PDO driver will return new PDO subclasses (e.g.,Pdo\MysqlorPdo\Pgsql) instead of the basePDOclass.Initialize the SQL QueryBuilder
4.4.xYou can access the
QueryBuilderby callingcreateQueryBuilder()on aDoctrine\DBAL\Connectioninstance. TheQueryBuilderobject provides methods to programmatically build SQL statements forSELECT,INSERT,UPDATE, andDELETEqueries.<?php $conn = DriverManager::getConnection([/*..*/]); $queryBuilder = $conn->createQueryBuilder();Update Statement parameter binding (DBAL 3.4+)
4.4.xWhen upgrading from 3.4, note the following deprecations regarding statement execution:
Statement::bindParam()andDriver Statement::bindParam()are deprecated. UsebindValue()instead.- Not passing a
$typetoStatement::bindParam()or::bindValue()is deprecated. Always pass the corresponding parameter type. - Passing
$paramsdirectly toStatement::execute(),Statement::executeQuery(), orStatement::executeStatement()is deprecated. UsebindParam()orbindValue()to bind parameters before execution.
Upgrade to 3.8: Replace deprecated QueryBuilder reset methods
4.4.xThe methods
QueryBuilder::resetQueryParts()andQueryBuilder::resetQueryPart()are deprecated. Use the following specific replacements based on the part you wish to reset:$queryPartNamesuggested replacement 'select'Call select()with a new set of columns.'distinct'distinct(false)'where'resetWhere()'groupBy'resetGroupBy()'having'resetHaving()'orderBy'resetOrderBy()'values'Call values()with a new set of values.Upgrade to 3.5: Replace deprecated extension via Doctrine Event Manager
4.4.xExtending library behavior via the Doctrine Event Manager is deprecated.
Connection/Platform Events:
AbstractPlatform::$_eventManager,getEventManager(),setEventManager()Connection::$_eventManager,getEventManager()
Connection
postConnectreplacements:OracleSessionInit$\rightarrow$Doctrine\DBAL\Driver\OCI8\Middleware\InitializeSessionSQLiteSessionInit$\rightarrow$Doctrine\DBAL\Driver\AbstractSQLiteDriver\Middleware\EnableForeignKeysSQLSessionInit$\rightarrow$ Implement a custom middleware.
Transaction/Schema Events:
- For transaction events (
onTransactionBegin, etc.), implement a driver middleware or a custom wrapper connection. - For schema definition/manipulation events, use a custom schema manager or extend the platform class directly.
Upgrade to 3.6: Replace deprecated 'url' connection parameter
4.4.xThe
urlparameter inDriverManager::getConnection()is deprecated. Use the newDsnParserto parse a database URL into connection parameters.Before:
$connection = DriverManager::getConnection( ['url' => 'mysql://my-user:t0ps3cr3t@my-host/my-database'] );After:
$dsnParser = new DsnParser(['mysql' => 'pdo_mysql']); $connection = DriverManager::getConnection( $dsnParser->parse('mysql://my-user:t0ps3cr3t@my-host/my-database') );Handle identifier quoting and case sensitivity
4.4.xThe object name parser no longer implicitly quotes identifiers for reserved keywords (e.g.,
select) or unquoted identifiers preceded by a quoted identifier (e.g.,"inventory".product).To ensure the original case of an identifier is preserved on platforms like PostgreSQL, Oracle, or IBM DB2, you must explicitly quote it (e.g., use
"select"instead ofselect).Migrate from DBALException to Exception (v3.0)
4.4.xWhen upgrading to version 3.0, note that theDoctrine extbackslash DBAL extbackslash DBALExceptionclass has been renamed toDoctrine extbackslash DBAL extbackslash Exception.