Skip to content

@MappedSuperclass vs. abstract class #1050

Open
@Jan0707

Description

@Jan0707

Given we have an abstract class which is declared a MappedSuperclass like this:

/**
 * @MongoDB\MappedSuperclass(repositoryClass="EventBundle\Repository\MongoDB\ReportRepository")
 * @MongoDB\DiscriminatorField(fieldName="type")
 * @MongoDB\DiscriminatorMap({
 *      "royalty_batch" = "TheaterBundle\Document\Event\Report\RoyaltyBatchReport",
 *      "royalty"       = "TheaterBundle\Document\Event\Report\RoyaltyReport",
 *      "tour"          = "TheaterBundle\Document\Event\Report\TourReport"
 * })
 * @Serializer\ExclusionPolicy("all")
 */
abstract class AbstractReport implements ReportInterface, ContextableInterface, TimestampableInterface

And that we have several child classes like:

/**
 * @MongoDB\Document(collection="reports")
 * @Serializer\ExclusionPolicy("all")
 */
class TourReport extends AbstractReport

In order to work with the child documents we get the repository (as a service) via the abstract class (because we should not have to declare a custom repository for each child class):

    event.repository.report:
        class: Doctrine\Common\Persistence\ObjectRepository
        factory_service: doctrine_mongodb
        factory_method: getRepository
        arguments:
            - 'EventBundle\Document\Report\AbstractReport'

In which case the $repository->findAll() yields no result.

The only fix I could come up with is to NOT declare the MappedSuperclass an abstract class and to declare the repository via the Document annotation :

/**
 * @MongoDB\Document(collection="reports", repositoryClass="EventBundle\Repository\MongoDB\ReportRepository")
 * @MongoDB\MappedSuperclass()
 * @MongoDB\DiscriminatorField(fieldName="type")
 * @MongoDB\DiscriminatorMap({
 *      "royalty_batch" = "TheaterBundle\Document\Event\Report\RoyaltyBatchReport",
 *      "royalty"       = "TheaterBundle\Document\Event\Report\RoyaltyReport",
 *      "tour"          = "TheaterBundle\Document\Event\Report\TourReport"
 * })
 * @Serializer\ExclusionPolicy("all")
 */
class AbstractReport implements ReportInterface, ContextableInterface, TimestampableInterface

I am slightly confused. Why can a MappedSuperclas NOT be an abstract class ? Or can it ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions