Humus Doctrine (recursive) Hydrator

1.1.0 2013-07-07 11:33 UTC


Humus Doctrine Hydrator

Dependency Status Total Downloads Latest Stable Version Latest Unstable Version

A Hydrator implementing Zend\Stdlib\Hydrator\HydratorInterface, completely based on Doctrine\Common

  • can return cloned object
  • let's you control what field should be extracted (recursively)
  • can flat single keys


  1. Add "prolic/humus-doctrine-hydrator": "dev-master" to your composer.json
  2. Run php composer.phar install


Let's assume you want to hydrate an array to an object, and then you want to extract all it's internals

Example Class: "User", has relationships to his "Locale", "Language" and "Address". The address entity has also a relation to "Country".

$hydrator = new \Humus\Doctrine\Hydrator\Hydrator(
    $context->get('EntityManager'), // the entity manager
    true, // yes, we want a cloned object, not the original one
        'locale' => array(
        'preferred_contact_type' => array(
        'address' => array(
            'country' => array(
        'spoken_languages' => array(
    ), // we describe, what should be returned by extract, otherwise it could be that
       // a) We dump the Unit of Work
       // b) We dump half of the database
    false //do not flat single keys

// we got some data
$data = array(
    'id' => 5,

// we get the cloned address object,
// just switch the clone argument in constructor to false, in order to get the real object back
$address = $hydrator->hydrate($data, new \Application\Entity\Contact());

$result = $hydrator->extract($address);

// output:
array(6) {
  string(4) "male"
  string(20) "Sascha-Oliver Prolic"
  array(1) {
    string(10) "Deutsch DE"
  array(4) {
    string(13) "Sample Street"
    string(5) "00000"
    string(6) "Berlin"
    array(2) {
      string(7) "Germany"
  array(1) {
    array(1) {

// Please note how the output changes (watch for locale) when you switch the "flatSingleKeys" flag to true
// in the constructor of the hydrator