treehouselabs / entity-merger
An entity merger
Installs: 1 589
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.4
- doctrine/orm: ~2.2,>=2.4.3
- jms/serializer: ~0.16
- symfony/symfony: ~2.5
Requires (Dev)
- doctrine/doctrine-bundle: ~1.2
- jms/serializer-bundle: ~0.12
- phpunit/phpunit: ~4.1
This package is not auto-updated.
Last update: 2020-01-24 14:47:43 UTC
README
Entity Merger is a library that can merge two entities.
Usage
Define as a service:
tree_house.entity_merger: class: TreeHouse\EntityMerger\EntityMerger arguments: - @jms_serializer - @doctrine - @jms_serializer.metadata_factory
Then use it as followed:
$merger = $container->get('tree_house.entity_merger'); $original = new Entity(); $original->setTitle('A'); var_dump($original->getAuthor()); // output: NULL $update = new Entity(); $update->setTitle('B'); $update->setAuthor('TreeHouse'); $original = $merger->merge($original, $update); var_dump($original->getTitle()) // output: "B" var_dump($original->getAuthor()); // output: "TreeHouse"
To update null values, you can hint the merger to do this:
$merger = $container->get('tree_house.entity_merger'); $original = new Entity(); $original->setTitle('A'); $update = new Entity(); $update->setTitle(null); $update->setAuthor('TreeHouse'); $original = $merger->merge($original, $update, null, true); var_dump($original->getTitle()) // output: NULL var_dump($original->getAuthor()); // output: "TreeHouse"
It is also possible to exclude fields by giving an array with property names which must be included:
$merger = $container->get('tree_house.entity_merger'); $original = new Entity(); $original->setTitle('A'); $update = new Entity(); $update->setTitle('B'); $update->setAuthor('TreeHouse'); // include the given fields, make sure to camelcase the fieldnames when needed $exclusionStrategy = new \TreeHouse\EntityMerger\Serializer\Exclusion\FieldsExclusionStrategy([ 'title' ]); $original = $merger->merge($original, $update, $exclusionStrategy); var_dump($original->getTitle()) // output: "B" var_dump($original->getAuthor()); // output: NULL
It is also possible to pass an array with fields and values:
$merger = $container->get('tree_house.entity_merger'); $original = new Entity(); $original->setTitle('A'); $original->setAuthor('TreeHouse'); $update = ['title' => 'new title']; $original = $merger->merge($original, $update); var_dump($original->getTitle()); // output: "new title" var_dump($original->getAuthor()); // output: "TreeHouse"