score:3

Accepted answer

Cascade operations are performed in memory. That means collections and related entities are fetched into memory, even if they are still marked as lazy when the cascade operation is about to be performed.

To rely on the database level cascade operations for the delete operation instead, you can configure each join column with the onDelete option by setting it CASCADE.

When using the orphanRemoval=true option Doctrine makes the assumption that the entities are privately owned and will NOT be reused by other entities. If you neglect this assumption your entities will get deleted by Doctrine even if you assigned the orphaned entity to another one.

Based on your template, you can try:

oneToMany:
  products:
    targetEntity: PostEntity
    mappedBy: post_template
    joinColumn:
      name: id
      referencedColumnName: template_id
      onDelete: CASCADE


manyToOne:
  template:
    targetEntity: PostTemplateEntity
    inversedBy: products
    cascade: ["remove"]
    joinColumn:
      name: template_id
      referencedColumnName: id

In many-to-many relations I have no idea based on yaml configuration. Only I know is manually, such as:

foreach ($user->getRoles() as $role) {
    $user->removeRole($role)
}
$em->remove($user);
$em->flush();

Related Query

More Query from same tag