title: Doctrine2 export entity to array
body: <p>I have <code>Product</code> entity with many-to-one to <code>Category</code> entity. I need store <code>Product</code> in session. First of all i try to implement <code>\Serializable</code> interface on Product. How should i serialize my related <code>Category</code> entity? Should i also implement <code>\Serializable</code> interface?</p>
  
  <p>I read, that serialization in doctrine is very pain operation and i think about this:</p>
  
  <p>Can we get raw values from entity? Exactly that data, which stored in database. If we can get this values, we can store it anywhere and recreate object!</p>
  
  <p>I read doctrine2 code and find method <code>Doctrine\ORM\Internal\Hydration\ObjectHydrator:hydrateRowData</code> but it's protected. Is there any public api for doing this?</p>
  
  <h1>Update:</h1>
  
  <p>I just copypaste and integrate some code from BasicEntityPersister and it seems to work.</p>
  
  <pre><code>    $product = $productsRepository-&gt;find($id);
  
      if (!$product) {
          throw $this-&gt;createNotFoundException('No product found for id ' . $id);
      }
  
      $uow = $em-&gt;getUnitOfWork();
      $entityPersister = $uow-&gt;getEntityPersister(get_class($product));
      $classMetadata = $entityPersister-&gt;getClassMetadata();
  
      $originalData = $uow-&gt;getOriginalEntityData($product);
  
      $result = array();
      foreach ($originalData as $field =&gt; $value) {
          if (isset($classMetadata-&gt;associationMappings[$field])) {
              $assoc = $classMetadata-&gt;associationMappings[$field];
  
              // Only owning side of x-1 associations can have a FK column.
              if ( ! $assoc['isOwningSide'] || ! ($assoc['type'] &amp; \Doctrine\ORM\Mapping\ClassMetadata::TO_ONE)) {
                  continue;
              }
  
              if ($value !== null) {
                  $newValId = $uow-&gt;getEntityIdentifier($value);
              }
  
              $targetClass = $em-&gt;getClassMetadata($assoc['targetEntity']);
              $owningTable = $entityPersister-&gt;getOwningTable($field);
  
              foreach ($assoc['joinColumns'] as $joinColumn) {
                  $sourceColumn = $joinColumn['name'];
                  $targetColumn = $joinColumn['referencedColumnName'];
  
                  if ($value === null) {
                      $result[$owningTable][$sourceColumn] = null;
                  } else if ($targetClass-&gt;containsForeignIdentifier) {
                      $result[$owningTable][$sourceColumn] = $newValId[$targetClass-&gt;getFieldForColumn($targetColumn)];
                  } else {
                      $result[$owningTable][$sourceColumn] = $newValId[$targetClass-&gt;fieldNames[$targetColumn]];
                  }
              }
          } elseif (isset($classMetadata-&gt;columnNames[$field])) {
              $columnName = $classMetadata-&gt;columnNames[$field];
              $result[$entityPersister-&gt;getOwningTable($field)][$columnName] = $value;
          }
      }
  
      print_r($result);
  </code></pre>
  
  <p>In <code>$result</code> we have raw values. Now we need way how to create object by this array</p>