Created
January 6, 2014 10:06
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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