2017 © Pedro Peláez
 

library doctrine-entity-merger

Add hint MERGE_ENTITY to merge fields retrieved by many queries

image

steevanb/doctrine-entity-merger

Add hint MERGE_ENTITY to merge fields retrieved by many queries

  • Thursday, May 24, 2018
  • by kujaff
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,151 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 7 Versions
  • 12 % Grown

The README.md

version doctrine php Lines Total Downloads SensionLabsInsight Scrutinizer, (*1)

doctrine-entity-merger

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

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

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 :, (*4)

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, (*5)

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) :, (*6)

steevanb/doctrine-events, (*7)

Add it to your composer.json :, (*8)

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

Add EntityMergerSubscriber :, (*9)

$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 :, (*10)

$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 : ```php, (*11)

app/AppKernel.php

use Doctrine\ORM\EntityManagerInterface; use steevanb\DoctrineEntityMerger\QueryHint; use steevanb\DoctrineEntityMerger\EventSubscriber\EntityMergerSubscriber;, (*12)

class AppKernel { public function boot() { parent::boot();, (*13)

    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());
        }
    }
}

}, (*14)

The Versions

24/05 2018

dev-master

9999999-dev

Add hint MERGE_ENTITY to merge fields retrieved by many queries

  Sources   Download

MIT

The Requires

 

24/05 2018

1.0.5

1.0.5.0

Add hint MERGE_ENTITY to merge fields retrieved by many queries

  Sources   Download

MIT

The Requires

 

06/10 2017

1.0.4

1.0.4.0

Add hint MERGE_ENTITY to merge fields retrieved by many queries

  Sources   Download

MIT

The Requires

 

05/10 2017

1.0.3

1.0.3.0

Add hint MERGE_ENTITY to merge fields retrieved by many queries

  Sources   Download

MIT

The Requires

 

04/10 2017

1.0.2

1.0.2.0

Add hint MERGE_ENTITY to merge fields retrieved by many queries

  Sources   Download

MIT

The Requires

 

21/09 2017

1.0.1

1.0.1.0

Add hint MERGE_ENTITY to merge fields retrieved by many queries

  Sources   Download

MIT

The Requires

 

14/12 2016

1.0.0

1.0.0.0

Add hint MERGE_ENTITY to merge fields retrieved by many queries

  Sources   Download

MIT

The Requires