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

Open
opened 2026-01-22 15:19:48 +01:00 by admin · 0 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:48 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5845