Combine Single-Table-Inheritance and Embeddables (=Single-Field-Inheritance?) #5848

Closed
opened 2026-01-22 15:19:50 +01:00 by admin · 4 comments
Owner

Originally created by @MarkusRodler on GitHub (Jan 14, 2018).

Originally assigned to: @Ocramius on GitHub.

Hello,

I want to use some sort of Single-Table-Inheritance (or maybe better: Single-Field-Inheritance).

PHP Example:
I have an abstract class AbstractDifficulty.
This AbstractDifficulty has three concrete classes (HardDifficulty, MediumDifficulty and EasyDifficulty)
And I have a class called Job. Every Job has a title and one difficulty.
My database schema should therefore look like this:

job
- title (string)
- difficulty (string (easy, medium, hard) or int (1,2,3))

In my php classes I thought it would be enough to have this:

/**
 * @Embeddable
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="difficulty", type="integer")
 * @DiscriminatorMap({"1"="EasyDifficulty", "5"="MediumDifficulty", "10"="HardDifficulty"})
 */
abstract class AbstractDifficulty implements DifficultyInterface
{
    /**
     * @var string
     */
    protected $difficultyName;
}

/**
 * @Embeddable
 */
final class EasyDifficulty extends AbstractDifficulty
{
    public function __construct()
    {
        $this->difficultyName = 'Easy Difficulty';
    }
}

/**
 * @Entity
 * @Table(name="job")
 */
class Job
{
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     */
    private $id = 0;
    
    /**
     * @var RecipeTitle
     * @Embedded(class="RecipeTitle", columnPrefix=false)
     */
    private $title;

    /**
     * @var DifficultyInterface
     * @Embedded(class="AbstractDifficulty", columnPrefix=false)
     */
    private $difficulty;
}

But that is not working. Doctrine does not generate a field "difficulty" in the job table. Maybe the two features (Embeddables and SingleTable-Inheritance) are not combinable. But if they are combinable then it would be great if someone can give me a hint.

Thanks!

Originally created by @MarkusRodler on GitHub (Jan 14, 2018). Originally assigned to: @Ocramius on GitHub. Hello, I want to use some sort of Single-Table-Inheritance (or maybe better: Single-Field-Inheritance). PHP Example: I have an abstract class AbstractDifficulty. This AbstractDifficulty has three concrete classes (HardDifficulty, MediumDifficulty and EasyDifficulty) And I have a class called Job. Every Job has a title and one difficulty. My database schema should therefore look like this: ``` job - title (string) - difficulty (string (easy, medium, hard) or int (1,2,3)) ``` In my php classes I thought it would be enough to have this: ```php /** * @Embeddable * @InheritanceType("SINGLE_TABLE") * @DiscriminatorColumn(name="difficulty", type="integer") * @DiscriminatorMap({"1"="EasyDifficulty", "5"="MediumDifficulty", "10"="HardDifficulty"}) */ abstract class AbstractDifficulty implements DifficultyInterface { /** * @var string */ protected $difficultyName; } /** * @Embeddable */ final class EasyDifficulty extends AbstractDifficulty { public function __construct() { $this->difficultyName = 'Easy Difficulty'; } } /** * @Entity * @Table(name="job") */ class Job { /** * @Id * @Column(type="integer") * @GeneratedValue */ private $id = 0; /** * @var RecipeTitle * @Embedded(class="RecipeTitle", columnPrefix=false) */ private $title; /** * @var DifficultyInterface * @Embedded(class="AbstractDifficulty", columnPrefix=false) */ private $difficulty; } ``` But that is not working. Doctrine does not generate a field "difficulty" in the job table. Maybe the two features (Embeddables and SingleTable-Inheritance) are not combinable. But if they are combinable then it would be great if someone can give me a hint. Thanks!
admin added the Can't FixQuestion labels 2026-01-22 15:19:50 +01:00
admin closed this issue 2026-01-22 15:19:50 +01:00
Author
Owner

@Ocramius commented on GitHub (Jan 14, 2018):

That's because you either map the column as a field or as a discriminator column: doing both is not supported.

@Ocramius commented on GitHub (Jan 14, 2018): That's because you either map the column as a field or as a discriminator column: doing both is not supported.
Author
Owner

@Ocramius commented on GitHub (Jan 14, 2018):

See also https://github.com/doctrine/doctrine2/issues/5439

@Ocramius commented on GitHub (Jan 14, 2018): See also https://github.com/doctrine/doctrine2/issues/5439
Author
Owner

@Ocramius commented on GitHub (Jan 14, 2018):

Ah, by the way, embeddable entities don't support inheritance - that was never tested, so it should probably be added as a separate validation issue (metadata validation should fail)

@Ocramius commented on GitHub (Jan 14, 2018): Ah, by the way, embeddable entities don't support inheritance - that was never tested, so it should probably be added as a separate validation issue (metadata validation should fail)
Author
Owner

@MarkusRodler commented on GitHub (Jan 15, 2018):

@Ocramius Thanks for the real quick reply!
But I have one question. Wouldn't it be a useful feature to combine those two features?
Without it, I have to modify my Business-Models in order to make it work with doctrine.

@MarkusRodler commented on GitHub (Jan 15, 2018): @Ocramius Thanks for the real quick reply! But I have one question. Wouldn't it be a useful feature to combine those two features? Without it, I have to modify my Business-Models in order to make it work with doctrine.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5848