Skip to content
Search

Doctrine Bulk

Doctrine DBAL Bulk is a Flow PHP library for bulk operations with the Doctrine Database Abstraction Layer (DBAL). It handles bulk inserts, updates and deletes. The library hides the complex parts of bulk operations behind a small API.

Installation

For the installation instructions, read the installation page.

Usage Examples

Insert:

<?php

use Flow\Doctrine\Bulk\{Bulk, BulkData};

Bulk::create()->insert(
    $dbalConnection,
    'users',
    new BulkData([
        ['id' => 1, 'name' => 'Name One', 'description' => 'Description One'],
        ['id' => 2, 'name' => 'Name Two', 'description' => 'Description Two'],
        ['id' => 3, 'name' => 'Name Three', 'description' => 'Description Three'],
    ]),
);

Column types

Without types, Bulk reads the column types of the table from the database, once for each table and Bulk instance. To bind with exactly your types, give them to BulkData:

<?php

use Doctrine\DBAL\Types\{Type, Types};
use Flow\Doctrine\Bulk\{Bulk, BulkData};

Bulk::create()->insert(
    $dbalConnection,
    'users',
    new BulkData(
        [
            ['id' => 1, 'name' => 'Name One', 'created_at' => new \DateTimeImmutable()],
            ['id' => 2, 'name' => 'Name Two', 'created_at' => new \DateTimeImmutable()],
        ],
        [
            'id' => Type::getType(Types::INTEGER),
            'name' => Type::getType(Types::STRING),
            'created_at' => Type::getType(Types::DATETIME_IMMUTABLE),
        ],
    ),
);

Update (PostgreSQL):

<?php

use Flow\Doctrine\Bulk\{Bulk, BulkData};
use Flow\Doctrine\Bulk\Dialect\PostgreSQLUpdateOptions;

Bulk::create()->update(
    $dbalConnection,
    'users',
    new BulkData([
        ['id' => 1, 'name' => 'Name One', 'description' => 'Description One'],
        ['id' => 2, 'name' => 'Name Two', 'description' => 'Description Two'],
    ]),
    new PostgreSQLUpdateOptions(primaryKeyColumns: ['id'], updateColumns: ['name']),
);

Bulk::delete() removes the rows that match all columns of BulkData.

Supported Dialects

  • PostgreSQL
  • MySQL / MariaDB
  • SQLite

Adding new Dialects

A Dialect builds the SQL of one insert, update or delete. It uses a BulkData and the InsertOptions / UpdateOptions implementation of the dialect (PostgreSQLInsertOptions, MySQLInsertOptions, SqliteInsertOptions, ...).

DbalPlatform picks the Dialect for a Doctrine DBAL platform. DbalQueryFactory is the QueryFactory implementation. It asks the Dialect for the query.

Found a typo or an outdated section? Edit this page on GitHub


Contributors

Built in the open.

Join us on GitHub
scroll back to top