浏览代码

Added support detaching an object from the GraphNavigator to be able re-accept this object.

Deni 13 年之前
父节点
当前提交
65ede16b52
共有 2 个文件被更改,包括 14 次插入0 次删除
  1. 12 0
      Serializer/GraphNavigator.php
  2. 2 0
      Serializer/Handler/DoctrineOrmProxyHandler.php

+ 12 - 0
Serializer/GraphNavigator.php

@@ -20,6 +20,7 @@ namespace JMS\SerializerBundle\Serializer;
 
 use JMS\SerializerBundle\Metadata\ClassMetadata;
 use Metadata\MetadataFactoryInterface;
+use JMS\SerializerBundle\Exception\InvalidArgumentException;
 use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface;
 
 final class GraphNavigator
@@ -126,6 +127,17 @@ final class GraphNavigator
         }
     }
 
+    public function detachObject($object)
+    {
+        if (null === $object) {
+            throw new InvalidArgumentException('$object cannot be null');
+        } else if (!is_object($object)) {
+            throw new InvalidArgumentException(sprintf('Expected an object to detach, given "%s".', gettype($object)));
+        }
+
+        $this->visiting->detach($object);
+    }
+
     private function afterVisitingObject(ClassMetadata $metadata, $object)
     {
         if (self::DIRECTION_SERIALIZATION === $this->direction) {

+ 2 - 0
Serializer/Handler/DoctrineOrmProxyHandler.php

@@ -30,7 +30,9 @@ class DoctrineOrmProxyHandler implements SerializationHandlerInterface
             $handled = true;
 
             $data->__load();
+            $visitor->getNavigator()->detachObject($data);
 
+            // pass the parent class not to load the metadata for the proxy class
             return $visitor->getNavigator()->accept($data, get_parent_class($data), $visitor);
         }