Permalink
Browse files

Add recursion to EntitySerializer

  • Loading branch information...
1 parent 14feac4 commit 803237404e7c3ab324999fda7d2693f081553048 @wildlyinaccurate wildlyinaccurate committed Jun 16, 2012
Showing with 44 additions and 4 deletions.
  1. +44 −4 library/Bgy/Doctrine/EntitySerializer.php
@@ -33,6 +33,16 @@ class EntitySerializer
*/
protected $_em;
+ /**
+ * @var int
+ */
+ protected $_recursionDepth = 0;
+
+ /**
+ * @var int
+ */
+ protected $_maxRecursionDepth = 0;
+
public function __construct($em)
{
$this->setEntityManager($em);
@@ -83,12 +93,21 @@ protected function _serializeEntity($entity)
}
} elseif ($mapping['isOwningSide'] && $mapping['type'] & ClassMetadata::TO_ONE) {
if (null !== $metadata->reflFields[$field]->getValue($entity)) {
- $data[$key] = $this->getEntityManager()
- ->getUnitOfWork()
- ->getEntityIdentifier(
+ if ($this->_recursionDepth < $this->_maxRecursionDepth) {
+ $this->_recursionDepth++;
+ $data[$key] = $this->_serializeEntity(
$metadata->reflFields[$field]
->getValue($entity)
);
+ $this->_recursionDepth--;
+ } else {
+ $data[$key] = $this->getEntityManager()
+ ->getUnitOfWork()
+ ->getEntityIdentifier(
+ $metadata->reflFields[$field]
+ ->getValue($entity)
+ );
+ }
} else {
// In some case the relationship may not exist, but we want
// to know about it
@@ -133,5 +152,26 @@ public function toXml($entity)
{
throw new Exception('Not yet implemented');
}
-}
+ /**
+ * Set the maximum recursion depth
+ *
+ * @param int $maxRecursionDepth
+ * @return void
+ */
+ public function setMaxRecursionDepth($maxRecursionDepth)
+ {
+ $this->_maxRecursionDepth = $maxRecursionDepth;
+ }
+
+ /**
+ * Get the maximum recursion depth
+ *
+ * @return int
+ */
+ public function getMaxRecursionDepth()
+ {
+ return $this->_maxRecursionDepth;
+ }
+
+}

0 comments on commit 8032374

Please sign in to comment.