title: Doctrine ODM returns proxy object for base class instead of sub-classed document
body: <p>During my work on providing new functionality to my project, I decided to expand models. I decided to use base class, grouping common methods, with few sub-classes, all kept in a single collection.</p>
  
  <p>Base, abstract class:</p>
  
  <pre><code>/**
  * @MongoDB\Document(repositoryClass="EntryRepository")
  * @MongoDB\MappedSuperclass
  * @MongoDB\InheritanceType("SINGLE_COLLECTION")
  * @MongoDB\DiscriminatorField(fieldName="type")
  * @MongoDB\DiscriminatorMap({"entry"="Application_Model_Entry", "image"="Application_Model_Image", "movie"="Application_Model_Movie"})
  */
  abstract class Application_Model_Entry
  {
      abstract function foo();
  }
  </code></pre>
  
  <p>Some concrete class (I have few more of them, similar, but some are abstract):</p>
  
  <pre><code>&lt;?php
  /** @MongoDB\Document */
  class Application_Model_Image extends Application_Model_Entry
  {
      function foo()
      {
          return 'foo';
      }
  }
  </code></pre>
  
  <p>After that transition, I opened my index page, and an error was presented - PHP was trying to call an abstract method, as the object returned by Doctrine was a proxy of the <strong>base</strong> class. I thought it was because I renamed the collection from <code>Application_Model_Image</code> to <code>Application_Model_Entry</code> and internal object references (<code>$ref</code> field of <code>DBRef</code>) were still pointing to <code>Application_Model_Image</code>, but I noticed something funny: <strong>always</strong> first query result was a proper document of a concrete class, and the rest were base class proxies - after I removed the first object from the database, the second one became fine.</p>
  
  <p>On a single-object-showing page everything works fine, for all objects, so I thought maybe it's something with Zend Framework's pager class iterating over results, so I skipped the pager and dumped the objects directly from query. I printed out classes of 100 first results, and some of them had proper classes (<a href="http://pastebin.com/PePjnwPq" rel="nofollow">Snippet of that list on pastebin</a>). I looked into the DB, but I noticed nothing special about the data (<a href="http://pastebin.com/2gy4nqdd" rel="nofollow">working</a> vs <a href="http://pastebin.com/TT0g3zx1" rel="nofollow">not working</a>). I thought maybe it's something wrong with the references, but as I wrote above, the same object could become working if it was first on the result list.</p>
  
  <p>Any ideas or hints? I can debug more, but I need to be told where to look inside Doctrine's code.</p>