Skip to content

Instantly share code, notes, and snippets.

@b-b3rn4rd
Created January 6, 2014 10:06
Show Gist options
  • Save b-b3rn4rd/8280668 to your computer and use it in GitHub Desktop.
Save b-b3rn4rd/8280668 to your computer and use it in GitHub Desktop.
Custom hydrator for Doctrine2, allows to access array elements using getters, similar to the ObjectHydrator
<?php
namespace My\Doctrine\Hydration;
use Doctrine\ORM\Internal\Hydration\ArrayHydrator,
My\ArrayObject as ArrayObject;
/**
* Custom Doctrine2 hydrator, allows to access array properties using getters like
* in ObjectHydrator
*
*
* @author Bernard Baltrusaitis <[email protected]>
*/
class ArrayObjectHydrator extends ArrayHydrator
{
protected function hydrateAllData()
{
$result = parent::hydrateAllData();
return $this->toArrayObject($result);
}
protected function toArrayObject(array $result)
{
$return = new ArrayObject($result);
foreach ($result as $k => $v) {
if (is_array($v)) {
$v = $this->toArrayObject($v);
}
$return[$k] = $v;
}
return $return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment