title: Doctrine ODM (MongoDB) - Get a complete array of an object?
body: <p>iv'e got a problem to receive a <strong>complete array</strong> (with <strong>all the data of the embedded childs collections</strong> and objects) of my document. My document looks exactly like this one:</p>
  
  <pre><code>use Doctrine\Common\Collections\ArrayCollection;
  
  /** @Document(collection="user") */
  class User {
  
  /** @Id */
  protected $id;
  
  /** @String */
  protected $firstname;
  
  /** @String */
  protected $lastname;
  
  /** @EmbedMany(targetDocument="Email") */
  protected $email;
  
  /** @EmbedMany(targetDocument="Address") */
  protected $address;
  
  /** @EmbedMany(targetDocument="Subscription") */
  protected $subscription;
  
  
  /**
  * Construct the user
  *
  * @param   array $properties
  * @throws  User_Exception
  */
  public function __construct(array $properties = array()) {
  
      $this-&gt;email = new ArrayCollection();
      $this-&gt;address = new ArrayCollection();
      $this-&gt;subscription = new ArrayCollection();
  
      foreach($properties as $name =&gt; $value){
          $this-&gt;{$name} = $value;
      }
  
  }
  
  
  ...
  </code></pre>
  
  <hr>
  
  <p><strong>I need a complete array of an embedded collection to output the whole data and render it by json</strong>. My query looks like this:</p>
  
  <pre><code>$query = $this-&gt;_dbContainer-&gt;getDocumentManager()-&gt;createQueryBuilder('User')-&gt;field('deletedAt')-&gt;exists(false);                          
  $result = $query-&gt;field('id')-&gt;equals($id)-&gt;getQuery()-&gt;getSingleResult();
  </code></pre>
  
  <hr>
  
  <p>For example, if i call the <code>toArray()</code> function like this:</p>
  
  <pre><code>$array = $result-&gt;getSubscription()-&gt;toArray();
  print_r($array);
  </code></pre>
  
  <hr>
  
  <p>Then the output ist just an array on top level:</p>
  
  <pre><code>[0] =&gt; Object Subscription...
  [1] =&gt; Object Subscription...
  ...
  </code></pre>
  
  <hr>
  
  <p>How can i easily get an array like this?</p>
  
  <pre><code>[0] =&gt; array('subscriptionLabel' =&gt; 'value1', 'field' =&gt; 'value1', ...)
  [1] =&gt; array('subscriptionLabel' =&gt; 'value2', 'field' =&gt; 'value2', ...)
  ...
  </code></pre>
  
  <hr>
  
  <p>Are there any best practises or maybe some missing helper scripts to prevent something ugly like this code (how to handle child -> child -> child szenarios? ugly -> ugly ugly -> ugly ugly ugly -> ...):</p>
  
  <pre><code>$example = array();
  foreach($result-&gt;getSubscription() as $key =&gt; $subscription) {
      $example[$key]['subscriptionLabel'] = $subscription-&gt;getSubscriptionLabel();
      $example[$key]['field'] = $subscription-&gt;getField();
      ...
  }
  </code></pre>
  
  <p>Thanks a lot,
  Stephan</p>