Multiple CTI (Class Table Inheritance) among classes in Doctrine 2?

doctrine, doctrine-orm, symfony

Solution

It seems to me that you don't actually need the table "message". If that's the case, you should define Message as a mapped super class

<?php

namespace Your\Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\MappedSuperClass
 *
 */
abstract class MappedSuperClassMessage
{
  /**
   * @var integer $id
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * Get id
   *
   * @return integer
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * Everything else you want the subclasses to have
   */
}

Now the only CTI you need to set up is for the SMS classes.

/**
 * @ORM\Entity
 * @ORM\Table(name="newsletter")
 */
class Newsletter extends MappedSuperClassMessage {}

/**
 * @ORM\Entity
 * @ORM\Table(name="sms")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="status", type="string")
 * @ORM\DiscriminatorMap({"sent"="SentSMS", "scheduled"="ScheduledSMS", 
 *     "failed"="FailedSMS"
 * })
 */
class SMS extends MappedSuperClassMessage {}

/**
 * @ORM\Entity
 * @ORM\Table(name="failed_sms")
 */
class FailedSMS extends SMS {}

This is not a tested reply, so I'm not sure if you'll have issues with it or not.

Problem

I'd like to have the following hierarchy in Doctrine2: ``` - Message - SMS - SentSMS - ScheduledSMS - FailedSMS - Newsletter - SystemComunication ``` But when i try to generate entities in Symfony 2 i get the following error: [Doctrine\ORM\Mappin\MappingException] Entity 'Acme\HelloBundle\Entity\FailedSMS' has a composite identifier but uses an ID generator other than manually assigning (Identity, Sequence). This is not supported. I think it's because `id` of `FailedSMS` (inherited from `Message`) it's in conflict with the fact that `FailedSMS` itself should have an assigned `id` in order to CTI (with `SMS`) to work. I'm asking for the moon or there is a way to make it work? A little overview of the hierarchy: ``` /** * @ORM\Entity * @ORM\Table(name="message") * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="type", type="string") * @ORM\DiscriminatorMap({"newsletter" = "Newsletter", "sms" = "SMS"}) */ class Message {} /** * @ORM\Entity * @ORM\Table(name="newsletter") */ class Newsletter extends Message {} /** * @ORM\Entity * @ORM\Table(name="sms") * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="status", type="string") * @ORM\DiscriminatorMap({"sent"="SentSMS", "scheduled"="ScheduledSMS", * "failed"="FailedSMS" * }) */ class SMS extends Message {} /** * @ORM\Entity * @ORM\Table(name="failed_sms") */ class FailedSMS extends SMS {} ```

Original source