PHPackages                             steevanb/doctrine-entity-merger - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. steevanb/doctrine-entity-merger

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

steevanb/doctrine-entity-merger
===============================

Add hint MERGE\_ENTITY to merge fields retrieved by many queries

1.0.5(8y ago)32.6k1MITPHP ^5.4.0 || ^7.0

Since Dec 14Compare

[ Source](https://github.com/steevanb/doctrine-entity-merger)[ Packagist](https://packagist.org/packages/steevanb/doctrine-entity-merger)[ RSS](/packages/steevanb-doctrine-entity-merger/feed)WikiDiscussions Synced today

READMEChangelog (5)Dependencies (2)Versions (7)Used By (1)

[![version](https://camo.githubusercontent.com/03dfada737aa675c3953e556941a7cd9c3035ba62cd684c6bae730e2f2e12a96/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f76657273696f6e2d312e302e352d677265656e2e737667)](https://github.com/steevanb/doctrine-entity-merger/tree/1.0.5)[![doctrine](https://camo.githubusercontent.com/600fd33c6d1b24dcd8f133d154a65ddd821d8a7491cd4a7177d9b85a3fae62b8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f637472696e652f6f726d2d253545322e352e302d626c75652e737667)](http://www.doctrine-project.org)[![php](https://camo.githubusercontent.com/18a177e74e5884307d10edd222e5d5c7b48af5e08c1051ecba0e2cb45cb35fab/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545352e342e36253230253743253743253230253545372e302d626c75652e737667)](http://www.php.net)[![Lines](https://camo.githubusercontent.com/694acdee117145ed7190e97490f58b64ef5f4509fa83e1225c887bded9d1bf78/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f64652532306c696e65732d3236342d677265656e2e737667)](https://camo.githubusercontent.com/694acdee117145ed7190e97490f58b64ef5f4509fa83e1225c887bded9d1bf78/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f64652532306c696e65732d3236342d677265656e2e737667)[![Total Downloads](https://camo.githubusercontent.com/cbf6fffc2d30d6c432eb30b72656cead0c5d843fc128feced8a99cab4e5c0edc/68747470733a2f2f706f7365722e707567782e6f72672f7374656576616e622f646f637472696e652d6576656e74732f646f776e6c6f616473)](https://camo.githubusercontent.com/cbf6fffc2d30d6c432eb30b72656cead0c5d843fc128feced8a99cab4e5c0edc/68747470733a2f2f706f7365722e707567782e6f72672f7374656576616e622f646f637472696e652d6576656e74732f646f776e6c6f616473)[![SensionLabsInsight](https://camo.githubusercontent.com/1a9e6d5b7dd95256b6ecaa08a3205764e1d3ac558c457239b9db6fdc15f6ead5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53656e73696f6e4c616273496e73696768742d706c6174696e756d2d627269676874677265656e2e737667)](https://insight.sensiolabs.com/projects/cf51b54f-77fa-459d-8a55-503732fef052/analyses/18)[![Scrutinizer](https://camo.githubusercontent.com/11e38b5483c9cf823cfef740b6511c74ee549d21b7d82f46894d14baffc0f200/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7374656576616e622f646f637472696e652d656e746974792d6d65726765722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/steevanb/doctrine-entity-merger/)

doctrine-entity-merger
----------------------

[](#doctrine-entity-merger)

When you use PARTIAL in DQL, you retrive only fields you need, instead of all Entity fields.

But, if you execute 2 PARTIAL on same entity, but not same fields, you will have this problem :

```
class FooEntity
{
    protected $id;
    protected $name;
    protected $description;
}

$foo1 = $repository
    ->createQueryBuilder('foo')
    ->select('PARTIAL foo.{id, name}')
    ->where('site.id = 1')
    ->getQuery()
    ->getSingleResult();

var_dump($foo1->getDescription()); // null, that's fine, description is not in PARTIAL

$foo2 = $repository
    ->createQueryBuilder('foo')
    ->select('PARTIAL foo.{id, name, description}')
    ->where('site.id = 1')
    ->getQuery()
    ->getSingleResult();

// $foo1 is same object as $foo2, cause Doctrine know first query hydrated $foo1
// so, when you ask same entity (same id in query) with 2nd query, Doctrine will execute SQL,
// but will not hydrate a new entity
// UnitOfWork will return instance of Foo who is already hydrated, with first query
var_dump(spl_object_hash($foo1) === spl_object_hash($foo2)); // true

// but, as Doctrine return $foo1 in 2nd query, your new field description will not be defined in $foo1
var_dump($foo1->getDescription()); // null, but we want it, cause it's defined in PARTIAL 2nd query
```

You can use *steevanb\\DoctrineEntityMerger\\QueryHint::MERGE\_ENTITY* to define description in *$foo1* :

```
use steevanb\DoctrineEntityMerger\QueryHint;

$foo1 = $repository
    ->createQueryBuilder('foo')
    ->select('PARTIAL foo.{id, name}')
    ->where('site.id = 1')
    ->getQuery()
    ->getSingleResult();

var_dump($foo1->getName()); // 'My name' for example
var_dump($foo1->getDescription()); // null, that's fine, description is not in PARTIAL

$foo1->setName('New name');
var_dump($foo1->getName()); // 'New name'

$foo2 = $repository
    ->createQueryBuilder('foo')
    ->select('PARTIAL foo.{id, description}')
    ->where('site.id = 1')
    ->getQuery()
    ->setHint(QueryHint::MERGE_ENTITY, true)
    ->getSingleResult();

var_dump($foo1->getName()); // 'New name', MERGE_ENTITY will not change Foo::$name value if it was already defined in another query before

var_dump($foo1->getDescription()); // 'My description'
```

[Changelog](changelog.md)

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

[](#installation)

As *doctrine-entity-merger* use *steevanb/doctrine-events*, see how to install it (composer dependecy is added here, you don't need to add it for steevanb/doctrine-events) :

[steevanb/doctrine-events](https://github.com/steevanb/doctrine-events)

Add it to your composer.json :

```
{
    "require": {
        "steevanb/doctrine-entity-merger": "^1.0.5",
    }
}
```

Add *EntityMergerSubscriber* :

```
$entityManager->getEventManager()->addEventSubscriber(
    new steevanb\DoctrineEntityMerger\EventSubscriber\EntityMergerSubscriber()
);
```

If you want to add MERGE\_ENTITY hint to all of your queries, you can do this :

```
$entityManager->getConfiguration()->setDefaultQueryHint(
    steevanb\DoctrineEntityMerger\QueryHint\QueryHint::MERGE_ENTITY,
    true
);
```

For example, if you are on a Symfony project, you can add it in *AppKernel* :

```
# app/AppKernel.php

use Doctrine\ORM\EntityManagerInterface;
use steevanb\DoctrineEntityMerger\QueryHint;
use steevanb\DoctrineEntityMerger\EventSubscriber\EntityMergerSubscriber;

class AppKernel
{
    public function boot()
    {
        parent::boot();

        foreach ($this->getContainer()->get('doctrine')->getManagers() as $manager) {
            if ($manager instanceof EntityManagerInterface) {
                // add hint MERGE_ENTITY to all your queries
                $manager->getConfiguration()->setDefaultQueryHint(QueryHint::MERGE_ENTITY, true);

                // add listener, who use steevanb/doctrine-events to change UnitOfWork::createEntity()
                // to take into account MERGE_ENTITY hint
                $manager->getEventManager()->addEventSubscriber(new EntityMergerSubscriber());
            }
        }
    }
}
```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity62

Established project with proven stability

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 ~105 days

Recently: every ~61 days

Total

6

Last Release

2959d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e59638521a0ca6412cd2fa506834e384d4bac92197159891d4ccfe8e4b3d61eb?d=identicon)[steevanb](/maintainers/steevanb)

### Embed Badge

![Health badge](/badges/steevanb-doctrine-entity-merger/health.svg)

```
[![Health](https://phpackages.com/badges/steevanb-doctrine-entity-merger/health.svg)](https://phpackages.com/packages/steevanb-doctrine-entity-merger)
```

###  Alternatives

[rcsofttech/audit-trail-bundle

Enterprise-grade, high-performance Symfony audit trail bundle. Automatically track Doctrine entity changes with split-phase architecture, multiple transports (HTTP, Queue, Doctrine), and sensitive data masking.

1175.2k](/packages/rcsofttech-audit-trail-bundle)

PHPackages © 2026

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