In my web application, which is built with Symfony2, contains the following entities:
/**
* @ORM\Entity
* @ORM\Table
*/
class Entity
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(name="id", type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="MappedSuperclass", mappedBy="entity", cascade={"persist", "remove"})
*/
private $mappedSuperclasses;
}
/**
* @ORM\MappedSuperclass
*/
abstract class MappedSuperclass
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(name="id", type="integer")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Entity", inversedBy="mappedSuperclasses")
* @ORM\JoinColumn(name="entity_id", referencedColumnName="id", nullable=false)
*/
protected $entity;
}
/**
* @ORM\Entity
* @ORM\Table(name="table_1")
*/
class Subclass1 extends MappedSuperclass
{
/**
* @ORM\Column(name="unique_member", type="string")
*/
private $uniqueMember;
}
/**
* @ORM\Entity
* @ORM\Table(name="table_2")
*/
class Subclass2 extends MappedSuperclass
{
/**
* @ORM\Column(name="unique_member", type="string")
*/
private $uniqueMember; // This is different from Subclass1
}
I'll explain this a bit. An Entity
has a collection of MappedSuperclass
. MappedSuperclass
is an abstract class which contains some common variables for its subclasses. Subclass1
and Subclass2
are subclasses of MappedSuperclass
. When an Entity
is removed from database, the items in $mappedSuperclasses
should be removed together, that's why the cascade={"persist", "remove"}
is set.
However, when I try to delete an Entity
, I got the following error:
ContextErrorException: Notice: Undefined index: entity in C:\project\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php line 1753
If I change the targetEntity
of Entity::$mappedSuperclasses
to Subclass1
or Subclass2
, it will work. Is my set up impossible to achieve ON DELETE CASCADE
? What am I missing?