Embedded + DiscriminatorMap #5781

Closed
opened 2026-01-22 15:17:35 +01:00 by admin · 2 comments
Owner

Originally created by @SerafimArts on GitHub (Nov 22, 2017).

Originally assigned to: @lcobucci on GitHub.

It does not turn out to befriending Embedded and DiscriminatorMap and I did not find it in the documentation.

An example of a code (The code is simplified):

Table:

- id       AI PRIMARY
- type     ENUM("GitHub", "Bitbucket", "FileSystem")
- branch   VARCHAR(...) DEFAULT "master"

Main Entity:

/** @ORM\Entity */
class Documentation 
{
    ...

    /** @ORM\Embedded(class=Repository::class) */
    protected $repository;
}

Embedded:

/**
 * @ORM\Embeddable()
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({
 *      "GitHub"     = GitHubRepository::class,
 *      "Bitbucket"  = BitbucketRepository::class,
 *      "FileSystem" = FileSystemRepository::class
 * })
 */
class Repository implements RepositoryInterface
{
    /** @ORM\Column(name="branch", type="string", length=255) */
    protected $branch = 'master';
}

///
/**
 * @ORM\Embeddable()
 */
class BitbucketRepository extends Repository implements RepositoryInterface
{
}

In my cozy inner world, the following should happen like this (example):

$repo = $documentation->getRepository(); 

$repo->type; // "GitHub"
\get_class($repo); // GitHubRepository::class

But... Errors emerge, such as:

[Doctrine\ORM\Mapping\MappingException]
  Entity 'App\Entity\Documentation\Repository' has to be part of the discriminator map of 'App\Entity\Documentation\Repository' to be properly mapped in the inheritance hierarchy. Alter
  natively you can make 'App\Entity\Documentation\Repository' an abstract class to avoid this exception from occurring.

or:

 The provided class "App\Entity\Documentation\Repository" is abstract, and can not be instantiate

...and the like.

I tried all the combinations of Embedded, InheritanceType, Entity, MappedSuperclass, etc =\

Originally created by @SerafimArts on GitHub (Nov 22, 2017). Originally assigned to: @lcobucci on GitHub. It does not turn out to befriending Embedded and DiscriminatorMap and I did not find it in the documentation. An example of a code (The code is simplified): Table: ``` - id AI PRIMARY - type ENUM("GitHub", "Bitbucket", "FileSystem") - branch VARCHAR(...) DEFAULT "master" ``` Main Entity: ```php /** @ORM\Entity */ class Documentation { ... /** @ORM\Embedded(class=Repository::class) */ protected $repository; } ``` Embedded: ```php /** * @ORM\Embeddable() * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorColumn(name="type", type="string") * @ORM\DiscriminatorMap({ * "GitHub" = GitHubRepository::class, * "Bitbucket" = BitbucketRepository::class, * "FileSystem" = FileSystemRepository::class * }) */ class Repository implements RepositoryInterface { /** @ORM\Column(name="branch", type="string", length=255) */ protected $branch = 'master'; } /// /** * @ORM\Embeddable() */ class BitbucketRepository extends Repository implements RepositoryInterface { } ``` In my cozy inner world, the following should happen like this (example): ``` $repo = $documentation->getRepository(); $repo->type; // "GitHub" \get_class($repo); // GitHubRepository::class ``` But... Errors emerge, such as: ``` [Doctrine\ORM\Mapping\MappingException] Entity 'App\Entity\Documentation\Repository' has to be part of the discriminator map of 'App\Entity\Documentation\Repository' to be properly mapped in the inheritance hierarchy. Alter natively you can make 'App\Entity\Documentation\Repository' an abstract class to avoid this exception from occurring. ``` or: ``` The provided class "App\Entity\Documentation\Repository" is abstract, and can not be instantiate ``` ...and the like. I tried all the combinations of Embedded, InheritanceType, Entity, MappedSuperclass, etc =\
admin added the Question label 2026-01-22 15:17:35 +01:00
admin closed this issue 2026-01-22 15:17:37 +01:00
Author
Owner

@lcobucci commented on GitHub (Nov 24, 2017):

@SerafimArts embeddables are not entities but value objects, which means that we don't support that. In order to achieve the result you want you should use a 1-to-1 or many-to-1 association (or not use inheritance at all).

Embedded objects are going to suffer a major redesign on v3.0 but for that these are the options we have.

@lcobucci commented on GitHub (Nov 24, 2017): @SerafimArts embeddables are not entities but value objects, which means that we don't support that. In order to achieve the result you want you should use a 1-to-1 or many-to-1 association (or not use inheritance at all). Embedded objects are going to suffer a major redesign on `v3.0` but for that these are the options we have.
Author
Owner

@lcobucci commented on GitHub (Nov 24, 2017):

I'll close the issue, please reopen if you have further questions 👍

@lcobucci commented on GitHub (Nov 24, 2017): I'll close the issue, please reopen if you have further questions 👍
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5781