raygo
raygo

Reputation: 1408

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!

Upvotes: 4

Views: 2187

Answers (2)

Aurimas
Aurimas

Reputation: 790

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 {}

Upvotes: 4

Peekmo
Peekmo

Reputation: 2903

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.

Upvotes: 8

Related Questions