Merge arrays into your object entities
Installs: 11 912
Open Issues: 1
- php: ^5.5|^7.0
- doctrine/orm: ^2.2.3
- symfony/serializer: ^2.3|^3.0
- jms/serializer: ~0.16
- phpunit/phpunit: ^4.3|^5.0
- satooshi/php-coveralls: ~0.6
- symfony/validator: ^2.8|^3.0
- jms/serializer: To merge entities with JMS serializer instead of Symfony one
⚠️ This package is unmaintained. Please look at better solutions, like serializers.
Entity/object merger for PHP apps.
Install with Composer, it's the best packages manager you can have:
composer require orbitale/entity-merger
- PHP 5.3.3 or more
- Any Doctrine
- Symfony Serializer (optional)
- JMS Serializer (optional)
EntityMerger only works with mapped objects. The properties must exist, and it cannot use magic
methods to retrieve datas.
use Orbitale\Component\EntityMerger\EntityMerger; $merger = new EntityMerger(); $object = new Object(); $object->setField(null); $postedDatas = array('field' => 'value'); $merger->merge($object, $postedDatas); echo $object->getField(); // Shows "value"
Using the Doctrine ORM ObjectManager
EntityMerger accepts a
Doctrine\ORM\ObjectManager as first argument, like the
EntityManager for instance.
This allows better and deeper merging when using ORM-mapped entities, by automatically using the Doctrine Metadatas
to detect property types.
If you are using the
EntityMerger in an application that uses Doctrine, it is highly recommended to inject the
ObjectManager to use your mapping as type reference for each of your class properties.
ObjectManager is used, the
EntityMerger will perform its own checks with its own metadatas manager, but it is
obviously less performant than the Doctrine one, because it cannot assume that you are attempting to merge an entity.
It instead makes its checks based on PHPDoc, class imports and default parameters, so be sure to perfectly write your
PHPDoc in your class if you do not want to use the
Using the Serializer
EntityMerger accepts a
Serializer as second argument.
It can both use the native Symfony Serializer
and the powerful JMS Serializer.
Note: If you are using Symfony <2.5 and still want to use the serializer, then you'll have to switch to the JMS'
one, or you may have some unexpected behavior because of the lack of the
Serializer allows you to merge two objects in the specified one by serializing the
into an array, and using it as values to merge in your object/entity.
Note: Of course you can use your own serialization method to inject the
dataObject as array in your merge process.
Merge two objects in the first one (must use any serializer):
use Orbitale\Component\EntityMerger\EntityMerger; $merger = new EntityMerger(null, $serializer); $baseObject = new Object; $baseObject->field = null; $anotherObject = new Object(); $anotherObject->field = 'value'; $merger->merge($baseObject, $anotherObject); echo $baseObject->field; // Shows "value"