PHPackages                             pharako/mysql-dbal - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. pharako/mysql-dbal

ActiveLibrary[Database &amp; ORM](/categories/database)

pharako/mysql-dbal
==================

MySQL extensions for Doctrine DBAL

1.0.0(5y ago)2416.2k4MITPHPPHP &gt;=7.2CI failing

Since Dec 6Pushed 1y ago1 watchersCompare

[ Source](https://github.com/pharako/mysql-dbal)[ Packagist](https://packagist.org/packages/pharako/mysql-dbal)[ RSS](/packages/pharako-mysql-dbal/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (5)Used By (0)

[![Build Status](https://camo.githubusercontent.com/2ccf7028af51bf95970a99cc600d702603df89b472c89582b5732e2527918c86/68747470733a2f2f7472617669732d63692e6f72672f70686172616b6f2f6d7973716c2d6462616c2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/pharako/mysql-dbal) [![Latest Stable Version](https://camo.githubusercontent.com/78c5acd007a23bb2889e282a24b8003bf9432a03b6d46d4101a482c085082b70/68747470733a2f2f706f7365722e707567782e6f72672f70686172616b6f2f6d7973716c2d6462616c2f762f737461626c65)](https://packagist.org/packages/pharako/mysql-dbal) [![Total Downloads](https://camo.githubusercontent.com/0ce638a3c1554ea4d7dbf5a7901ba85450aa5e486e088317c5793248a6b26972/68747470733a2f2f706f7365722e707567782e6f72672f70686172616b6f2f6d7973716c2d6462616c2f646f776e6c6f616473)](https://packagist.org/packages/pharako/mysql-dbal) [![Latest Unstable Version](https://camo.githubusercontent.com/295d90e12df37d25ec9fb3cf19a87989955710643ef28803ca1d6cbe66489961/68747470733a2f2f706f7365722e707567782e6f72672f70686172616b6f2f6d7973716c2d6462616c2f762f756e737461626c65)](https://packagist.org/packages/pharako/mysql-dbal) [![License](https://camo.githubusercontent.com/bae1b54dab4464b42037bc1392cda767f71750062eef95d43665a26d9b00922e/68747470733a2f2f706f7365722e707567782e6f72672f70686172616b6f2f6d7973716c2d6462616c2f6c6963656e7365)](https://packagist.org/packages/pharako/mysql-dbal)

MySQL DBAL
==========

[](#mysql-dbal)

MySQL extensions for [Doctrine DBAL](https://github.com/doctrine/dbal).

`Pharako\DBAL\Connection` is an extension of `Doctrine\DBAL\Connection`—all functionality you get from the latter is also contained in the former, with a few add-ons specific to databases compatible with MySQL:

- multiple inserts
- single and multiple *upserts* (update records if they exist, insert them otherwise)

Supported databases
-------------------

[](#supported-databases)

- MySQL
- MariaDB

Requirements
------------

[](#requirements)

PHP 8.0 and above. See the [releases page](https://github.com/pharako/mysql-dbal/releases) for previous versions that still work with PHP &lt; 8.0.

Installation
------------

[](#installation)

Install via Composer:

```
$ composer require pharako/mysql-dbal
```

Usage
-----

[](#usage)

### Instantiation and configuration

[](#instantiation-and-configuration)

Most PHP frameworks will have some sort of service injection functionality to help you with configuration, but nothing stops you from doing it by hand.

#### Manually

[](#manually)

```
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Driver\PDOMySql\Driver;
use Pharako\DBAL\Connection;

$params = [
    'dbname' => 'my_db',
    'host' => 'localhost',
    'user' => 'username',
    'password' => '***',
    'driver' => 'pdo_mysql'
];

$dbal = new Connection(
    $params,
    new Driver(),
    new Configuration(),
    new EventManager()
);
```

#### Symfony 2 and above

[](#symfony-2-and-above)

Just specify the DBAL connection class under `wrapper_class` in `config.yml`. All other configurations should remain the same:

```
doctrine:
    dbal:
        dbname: %database_name%
        host: %database_host%
        port: %database_port%
        user: %database_user%
        password: %database_password%
        driver: pdo_mysql
        wrapper_class: 'Pharako\DBAL\Connection'
```

You can read [Doctrine DBAL Configuration](http://symfony.com/doc/current/reference/configuration/doctrine.html#doctrine-dbal-configuration) for more information on `wrapper_class` and other options.

Extra functionality
-------------------

[](#extra-functionality)

Pharako's additional methods follow the structure of Doctrine's [data retrieval and manipulation](http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html) functionality, including [binding types](http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html#binding-types).

### Multiple inserts

[](#multiple-inserts)

You can insert multiple records with one call—this will hit the database only once:

```
$data = [
    [
        'name' => 'Foo',
        'family_name' => 'Bar'
    ],
    [
        'name' => 'Fuzz',
        'family_name' => 'Bazz'
    ]
];

$dbal->insert('my_table', $data);
```

Or, if you want to specify the types of the data to be inserted:

```
$dbal->insert('my_table', $data, [\PDO::PARAM_STR, \PDO::PARAM_STR]);
```

### Single and multiple upserts (update if present, insert if new)

[](#single-and-multiple-upserts-update-if-present-insert-if-new)

Before using this functionality, make sure you read [*Careful with those upserts*](#careful-with-those-upserts) below.

Building on the previous example and assuming the `name` field is a unique key in the table structure, the first two records will have their `family_name` fields updated to `Rab` and `Zabb`, respectively, and the last one will be inserted:

```
$data = [
    [
        'name' => 'Foo',
        'family_name' => 'Rab'
    ],
    [
        'name' => 'Fuzz',
        'family_name' => 'Zabb'
    ],
    [
        'name' => 'New',
        'family_name' => 'Foo'
    ]
];

$dbal->upsert('my_table', $data);
```

Again, this will hit the database only once.

If you want your upsert to update only a few columns and leave all the others untouched, you can pass it an array specifying those columns:

```
$data = [
    'who' => 'Them',
    'where' => 'There',
    'when' => 'Sometime',
    'why' => 'Because'
];

$dbal->upsert(
    'another_table',
    $data,
    [\PDO::PARAM_STR, \PDO::PARAM_STR, \PDO::PARAM_STR, \PDO::PARAM_STR],
    ['where', 'when']
);
```

In this example, if the upsert results in an update, only the `where` and `when` fields will be updated. If the upsert results in an insert, all fields will be included.

#### Careful with those upserts

[](#careful-with-those-upserts)

By and large, it is safe to execute upserts against tables of varied structures—those containing a single unique index, a multi-column unique index or even multiple unique indexes.

However, because upserts in MySQL are more involved than simple inserts and updates, you should **not** expect those methods to behave similarly in 100% of the cases (for example, `LAST_INSERT_ID()` in the context of an upsert may behave slightly differently than in that of an insert).

That's why the [official documentation](https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html) says that *"In general, you should try to avoid using an `ON DUPLICATE KEY UPDATE` clause on tables with multiple unique indexes"* and *"\[...\] an `INSERT ... ON DUPLICATE KEY UPDATE` statement against a table having more than one unique or primary key is also marked as unsafe."*

Despite that, upserts will work just as expected but in [edge case scenarios](http://bugs.mysql.com/bug.php?id=58637). If you want to play it extra safe, though, try to **tighten your tests** and make sure you get the expected results when the upsert updates your records as well as when it inserts them.

Development
-----------

[](#development)

If you want to test this package from your workstation, checkout the [development environment](https://github.com/pharako/mysql-dbal-dev).

Code contributions and bug reports are welcome. For pull requests, please use the `development` branch.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~469 days

Total

4

Last Release

2043d ago

Major Versions

0.0.3 → 1.0.02020-10-14

PHP version history (2 changes)0.0.1PHP &gt;=5.6

0.0.2PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/5c396d89a61a7593f7e76e4082f4a25737da24c47e0dc1193d70a712f8306d16?d=identicon)[mleczakm](/maintainers/mleczakm)

![](https://avatars.githubusercontent.com/u/217849365?v=4)[Claudio Bizzotto](/maintainers/claudiobizzotto)[@claudiobizzotto](https://github.com/claudiobizzotto)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")[![claudiobizzotto](https://avatars.githubusercontent.com/u/217849365?v=4)](https://github.com/claudiobizzotto "claudiobizzotto (4 commits)")[![mleczakm](https://avatars.githubusercontent.com/u/3474636?v=4)](https://github.com/mleczakm "mleczakm (2 commits)")[![GDCElBardan](https://avatars.githubusercontent.com/u/100760060?v=4)](https://github.com/GDCElBardan "GDCElBardan (1 commits)")

---

Tags

dbaldoctrinedoctrine-dbalmultiple-insertsmysqlphpupsertmysqldoctrinedbalmultipleinsertupsert

###  Code Quality

TestsCodeception

### Embed Badge

![Health badge](/badges/pharako-mysql-dbal/health.svg)

```
[![Health](https://phpackages.com/badges/pharako-mysql-dbal/health.svg)](https://phpackages.com/packages/pharako-mysql-dbal)
```

###  Alternatives

[fresh/doctrine-enum-bundle

Provides support of ENUM type for Doctrine2 in Symfony applications.

4636.8M12](/packages/fresh-doctrine-enum-bundle)[scienta/doctrine-json-functions

A set of extensions to Doctrine that add support for json query functions.

58723.9M36](/packages/scienta-doctrine-json-functions)[martin-georgiev/postgresql-for-doctrine

Extends Doctrine with native PostgreSQL support for arrays, JSONB, ranges, PostGIS geometries, text search, ltree, uuid, and 100+ PostgreSQL-specific functions.

4485.3M4](/packages/martin-georgiev-postgresql-for-doctrine)[flow-php/doctrine-dbal-bulk

Bulk inserts and updates for Doctrine DBAL

14295.2k1](/packages/flow-php-doctrine-dbal-bulk)[nettrine/dbal

Doctrine DBAL for Nette Framework

322.6M19](/packages/nettrine-dbal)[marktopper/doctrine-dbal-timestamp-type

Add the timestamp type for Doctrine/DBAL

49808.2k1](/packages/marktopper-doctrine-dbal-timestamp-type)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
