问题
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 classesMappedSuperclass
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