Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine Inheritance - Single_Table inheritade from Joined table

This is the configuration I want to have:

An Entity "Account" with a JOINED inheritance to two other Entities: "Author" and "AccountBackend".

Then I would want the "AccountBackend" to have a SINGLE_TABLE inheritance with other two Entities: "Administrator" and "FeaturedAuthor". This is the way I have them defined:

Account.php

/** 
 * @Entity (repositoryClass="Repositories\Account") 
 * @Table(name="accounts")  
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr", type="integer")
 * @DiscriminatorMap({"1"="Author","2"="AccountBackend"})
 * @HasLifecycleCallbacks
 */
class Account
{

Curator.php

/**
 * @Entity
 * @Table(name="accounts_author")
 */
class Author extends Account
{

AccountBackend.php

/** 
 * @Entity (repositoryClass="Repositories\AccountBackend") 
 * @Table(name="accounts_backend")  
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr", type="integer")
 * @DiscriminatorMap({"1"="FeaturedAuthor","2"="Administrator"})
 * @HasLifecycleCallbacks
 */
class AccountBackend extends Account
{

FeaturedAuthor.php

/**
 * @Entity
 */
class FeaturedAuthor extends AccountBackend
{

Administrator.php

/**
 * @Entity
 */
class Administrator extends AccountBackend
{

When I have them defined, when I try to do an update through the CLI it says

"Entity 'Entities\AccountBackend' has to be part of the discriminator map of 'Entities\Account' to be properly mapped in the inheritance hierachy. Alternatively you can make 'Entities\AccountBackend' an abstract class to avoid this exception from occuring."

I don't see anything wrong with the way I defined them, this is the first time that I try to have inheritance on an already inherited Entity. Any idea of whats wrong? Thanks!

like image 251
raygo Avatar asked Dec 20 '22 16:12

raygo


2 Answers

You need a case for your "AccountBackend" class in your @DiscriminatorMap

e.g

@DiscriminatorMap({"1"="FeaturedAuthor","2"="Administrator", "3"="AccountBackend"})

As explain in the Documentation

All entity classes that is part of the mapped entity hierarchy (including the topmost class) should be specified in the @DiscriminatorMap. In the case above Person class included.

like image 69
Peekmo Avatar answered Dec 22 '22 04:12

Peekmo


In case you came here & had problem with SINGLE_TABLE inheritance - possible that the problem occurs because your discriminator class is not abstract.

My case example (solution)

/**
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="entity", type="string")
 * @ORM\DiscriminatorMap({"product" = "ProductReview", "seller" = "SellerReview"})
 * @ORM\Table(name="reviews")
 */
abstract class Review {}

class ProductReview extends Review {}

class SellerReview extends Review {}
like image 45
Aurimas Avatar answered Dec 22 '22 05:12

Aurimas