Doctrine ODM returns proxy object for base class instead of sub-classed document

余生颓废 提交于 2020-01-24 21:50:09

问题


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.

Base, abstract class:

/**
* @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();
}

Some concrete class (I have few more of them, similar, but some are abstract):

<?php
/** @MongoDB\Document */
class Application_Model_Image extends Application_Model_Entry
{
    function foo()
    {
        return 'foo';
    }
}

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 base class. I thought it was because I renamed the collection from Application_Model_Image to Application_Model_Entry and internal object references ($ref field of DBRef) were still pointing to Application_Model_Image, but I noticed something funny: always 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.

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 (Snippet of that list on pastebin). I looked into the DB, but I noticed nothing special about the data (working vs not working). 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.

Any ideas or hints? I can debug more, but I need to be told where to look inside Doctrine's code.


回答1:


Per comment by jmikola above, I've removed Document annotation, and everything started to work smoothly. The strange results I had led me to look for an error in a wrong place, but thankfully I got a tip here.

For posterity:

  • Document annotation is for concrete classes
  • MappedSuperclass annotation is for abstract classes
  • If you receive proxy class objects in query results, it's possible you have a mixup in that department
  • You can indicate discriminator field/values even without Document annotation.


来源:https://stackoverflow.com/questions/15056946/doctrine-odm-returns-proxy-object-for-base-class-instead-of-sub-classed-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!