{"id":17797384,"url":"https://github.com/steevanb/doctrine-entity-merger","last_synced_at":"2025-03-17T04:31:13.514Z","repository":{"id":57059373,"uuid":"76350177","full_name":"steevanb/doctrine-entity-merger","owner":"steevanb","description":"Add hint MERGE_ENTITY to merge fields retrieved by many queries","archived":false,"fork":false,"pushed_at":"2018-05-24T10:16:15.000Z","size":10,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-27T17:36:24.658Z","etag":null,"topics":["doctrine","php","symfony"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/steevanb.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-13T10:50:49.000Z","updated_at":"2020-06-04T13:22:37.000Z","dependencies_parsed_at":"2022-08-24T07:40:12.140Z","dependency_job_id":null,"html_url":"https://github.com/steevanb/doctrine-entity-merger","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steevanb%2Fdoctrine-entity-merger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steevanb%2Fdoctrine-entity-merger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steevanb%2Fdoctrine-entity-merger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steevanb%2Fdoctrine-entity-merger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/steevanb","download_url":"https://codeload.github.com/steevanb/doctrine-entity-merger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243841211,"owners_count":20356443,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["doctrine","php","symfony"],"created_at":"2024-10-27T11:55:36.083Z","updated_at":"2025-03-17T04:31:13.211Z","avatar_url":"https://github.com/steevanb.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![version](https://img.shields.io/badge/version-1.0.5-green.svg)](https://github.com/steevanb/doctrine-entity-merger/tree/1.0.5)\n[![doctrine](https://img.shields.io/badge/doctrine/orm-^2.5.0-blue.svg)](http://www.doctrine-project.org)\n[![php](https://img.shields.io/badge/php-^5.4.6%20||%20^7.0-blue.svg)](http://www.php.net)\n![Lines](https://img.shields.io/badge/code%20lines-264-green.svg)\n![Total Downloads](https://poser.pugx.org/steevanb/doctrine-events/downloads)\n[![SensionLabsInsight](https://img.shields.io/badge/SensionLabsInsight-platinum-brightgreen.svg)](https://insight.sensiolabs.com/projects/cf51b54f-77fa-459d-8a55-503732fef052/analyses/18)\n[![Scrutinizer](https://scrutinizer-ci.com/g/steevanb/doctrine-entity-merger/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/steevanb/doctrine-entity-merger/)\n\ndoctrine-entity-merger\n---------------------\n\nWhen you use PARTIAL in DQL, you retrive only fields you need, instead of all Entity fields.\n\nBut, if you execute 2 PARTIAL on same entity, but not same fields, you will have this problem :\n```php\nclass FooEntity\n{\n    protected $id;\n    protected $name;\n    protected $description;\n}\n\n$foo1 = $repository\n    -\u003ecreateQueryBuilder('foo')\n    -\u003eselect('PARTIAL foo.{id, name}')\n    -\u003ewhere('site.id = 1')\n    -\u003egetQuery()\n    -\u003egetSingleResult();\n\nvar_dump($foo1-\u003egetDescription()); // null, that's fine, description is not in PARTIAL\n\n$foo2 = $repository\n    -\u003ecreateQueryBuilder('foo')\n    -\u003eselect('PARTIAL foo.{id, name, description}')\n    -\u003ewhere('site.id = 1')\n    -\u003egetQuery()\n    -\u003egetSingleResult();\n\n// $foo1 is same object as $foo2, cause Doctrine know first query hydrated $foo1\n// so, when you ask same entity (same id in query) with 2nd query, Doctrine will execute SQL,\n// but will not hydrate a new entity\n// UnitOfWork will return instance of Foo who is already hydrated, with first query\nvar_dump(spl_object_hash($foo1) === spl_object_hash($foo2)); // true\n\n// but, as Doctrine return $foo1 in 2nd query, your new field description will not be defined in $foo1\nvar_dump($foo1-\u003egetDescription()); // null, but we want it, cause it's defined in PARTIAL 2nd query\n```\n\nYou can use _steevanb\\DoctrineEntityMerger\\QueryHint::MERGE_ENTITY_ to define description in _$foo1_ :\n```php\nuse steevanb\\DoctrineEntityMerger\\QueryHint;\n\n$foo1 = $repository\n    -\u003ecreateQueryBuilder('foo')\n    -\u003eselect('PARTIAL foo.{id, name}')\n    -\u003ewhere('site.id = 1')\n    -\u003egetQuery()\n    -\u003egetSingleResult();\n\nvar_dump($foo1-\u003egetName()); // 'My name' for example\nvar_dump($foo1-\u003egetDescription()); // null, that's fine, description is not in PARTIAL\n\n$foo1-\u003esetName('New name');\nvar_dump($foo1-\u003egetName()); // 'New name'\n\n$foo2 = $repository\n    -\u003ecreateQueryBuilder('foo')\n    -\u003eselect('PARTIAL foo.{id, description}')\n    -\u003ewhere('site.id = 1')\n    -\u003egetQuery()\n    -\u003esetHint(QueryHint::MERGE_ENTITY, true)\n    -\u003egetSingleResult();\n\nvar_dump($foo1-\u003egetName()); // 'New name', MERGE_ENTITY will not change Foo::$name value if it was already defined in another query before\n\nvar_dump($foo1-\u003egetDescription()); // 'My description'\n```\n\n[Changelog](changelog.md)\n\nInstallation\n------------\n\nAs _doctrine-entity-merger_ use _steevanb/doctrine-events_, see how to install it\n(composer dependecy is added here, you don't need to add it for steevanb/doctrine-events) :\n\n[steevanb/doctrine-events](https://github.com/steevanb/doctrine-events)\n\nAdd it to your composer.json :\n```yml\n{\n    \"require\": {\n        \"steevanb/doctrine-entity-merger\": \"^1.0.5\",\n    }\n}\n```\n\nAdd _EntityMergerSubscriber_ :\n```php\n$entityManager-\u003egetEventManager()-\u003eaddEventSubscriber(\n    new steevanb\\DoctrineEntityMerger\\EventSubscriber\\EntityMergerSubscriber()\n);\n```\n\nIf you want to add MERGE_ENTITY hint to all of your queries, you can do this :\n\n```php\n$entityManager-\u003egetConfiguration()-\u003esetDefaultQueryHint(\n    steevanb\\DoctrineEntityMerger\\QueryHint\\QueryHint::MERGE_ENTITY,\n    true\n);\n```\n\nFor example, if you are on a Symfony project, you can add it in _AppKernel_ :\n```php\n# app/AppKernel.php\n\nuse Doctrine\\ORM\\EntityManagerInterface;\nuse steevanb\\DoctrineEntityMerger\\QueryHint;\nuse steevanb\\DoctrineEntityMerger\\EventSubscriber\\EntityMergerSubscriber;\n\nclass AppKernel\n{\n    public function boot()\n    {\n        parent::boot();\n        \n        foreach ($this-\u003egetContainer()-\u003eget('doctrine')-\u003egetManagers() as $manager) {\n            if ($manager instanceof EntityManagerInterface) {\n                // add hint MERGE_ENTITY to all your queries\n                $manager-\u003egetConfiguration()-\u003esetDefaultQueryHint(QueryHint::MERGE_ENTITY, true);\n\n                // add listener, who use steevanb/doctrine-events to change UnitOfWork::createEntity()\n                // to take into account MERGE_ENTITY hint\n                $manager-\u003egetEventManager()-\u003eaddEventSubscriber(new EntityMergerSubscriber());\n            }\n        }\n    }\n}\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteevanb%2Fdoctrine-entity-merger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsteevanb%2Fdoctrine-entity-merger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteevanb%2Fdoctrine-entity-merger/lists"}