Doctrine DBAL Documentation

repository·4.4.x·Indexed 27 days ago

https://github.com/doctrine/dbal

A 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.

Tokens
35.9K
Snippets
72
Records
253
Agent score
94%

What's inside Doctrine DBAL

  1. Overview of Doctrine DBAL

    4.4.x
    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.
  2. Understand the Doctrine DBAL Architecture

    4.4.x

    Doctrine 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_mysql or mysqli) using four core interfaces:

    • Doctrine\DBAL\Driver
    • Doctrine\DBAL\Driver\Connection
    • Doctrine\DBAL\Driver\Statement
    • Doctrine\DBAL\Driver\Result

    A Doctrine\DBAL\Driver implementation is responsible for translating connection parameters and acting as a factory for platform, schema manager, and exception converter components.

  3. Understand the Doctrine DBAL Type system

    4.4.x

    Doctrine 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.
  4. Upgrade to 3.10: Support for PHP 8.4 PDO subclasses

    4.4.x
    In version 3.10.2, support for new PDO subclasses introduced in PHP 8.4 was backported. When calling getNativeConnection() on a connection established through a PDO driver on PHP 8.4, you will receive instances of the new PDO subclasses (e.g., Pdo\Mysql or Pdo\Pgsql) instead of the generic PDO class.
  5. Upgrade to 4.2: Handle PDO subclasses on PHP 8.4

    4.4.x
    When using PHP 8.4, calling getNativeConnection() on a connection established through a PDO driver will return new PDO subclasses (e.g., Pdo\Mysql or Pdo\Pgsql) instead of the base PDO class.
  6. Initialize the SQL QueryBuilder

    4.4.x

    You can access the QueryBuilder by calling createQueryBuilder() on a Doctrine\DBAL\Connection instance. The QueryBuilder object provides methods to programmatically build SQL statements for SELECT, INSERT, UPDATE, and DELETE queries.

    <?php
    
    $conn = DriverManager::getConnection([/*..*/]);
    $queryBuilder = $conn->createQueryBuilder();
  7. Update Statement parameter binding (DBAL 3.4+)

    4.4.x

    When upgrading from 3.4, note the following deprecations regarding statement execution:

    1. Statement::bindParam() and Driver Statement::bindParam() are deprecated. Use bindValue() instead.
    2. Not passing a $type to Statement::bindParam() or ::bindValue() is deprecated. Always pass the corresponding parameter type.
    3. Passing $params directly to Statement::execute(), Statement::executeQuery(), or Statement::executeStatement() is deprecated. Use bindParam() or bindValue() to bind parameters before execution.
  8. Upgrade to 3.8: Replace deprecated QueryBuilder reset methods

    4.4.x

    The methods QueryBuilder::resetQueryParts() and QueryBuilder::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.
  9. Upgrade to 3.5: Replace deprecated extension via Doctrine Event Manager

    4.4.x

    Extending library behavior via the Doctrine Event Manager is deprecated.

    Connection/Platform Events:

    • AbstractPlatform::$_eventManager, getEventManager(), setEventManager()
    • Connection::$_eventManager, getEventManager()

    Connection postConnect replacements:

    • OracleSessionInit $\rightarrow$ Doctrine\DBAL\Driver\OCI8\Middleware\InitializeSession
    • SQLiteSessionInit $\rightarrow$ Doctrine\DBAL\Driver\AbstractSQLiteDriver\Middleware\EnableForeignKeys
    • SQLSessionInit $\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.
  10. Upgrade to 3.6: Replace deprecated 'url' connection parameter

    4.4.x

    The url parameter in DriverManager::getConnection() is deprecated. Use the new DsnParser to 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')
    );
  11. Handle identifier quoting and case sensitivity

    4.4.x

    The 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 of select).

  12. Migrate from DBALException to Exception (v3.0)

    4.4.x
    When upgrading to version 3.0, note that the Doctrine extbackslash DBAL extbackslash DBALException class has been renamed to Doctrine extbackslash DBAL extbackslash Exception.