0

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?

pikachu0
  • 802
  • 1
  • 12
  • 23

2 Answers2

0

I solved this problem by setting the ON DELETE action to database level:

/**
 * @ORM\Entity
 * @ORM\Table
 */
class Entity
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(name="id", type="integer")
     */
    private $id;
}

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

    /**
     * @ORM\ManyToOne(targetEntity="Entity")
     * @ORM\JoinColumn(name="entity_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
     */
    protected $entity;
}

Sources: [1] [2]

Community
  • 1
  • 1
pikachu0
  • 802
  • 1
  • 12
  • 23
0

Im answering year after the issue was resolved but I had the same problem. Error occurs when I tried to empty count arrayCollection.

So the solution was to check if $this->entity is array and then return its length.

hywak
  • 890
  • 1
  • 14
  • 27