On delete cascade with doctrine2

On delete cascade with doctrine2

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

On Delete Cascade with Doctrine2

When working with Doctrine ORM, there is a frequent disconnect between what you configure in your entities and how the database actually enforces referential integrity. A common scenario involves attempting to implement an ON DELETE CASCADE constraint so that removing a parent record automatically removes its associated children. If your schema shows the foreign key exists but lacks the cascade behavior, the issue almost always lies in the mapping configuration of the owning side of the relationship.

In the provided example, you have defined a ManyToOne relationship in the Child entity pointing to a Father. This is the correct approach because the father_id column resides on the child table, making it the child (or "owning") side of the association. However, the annotation:

@ORM\ManyToOne(targetEntity="Father", cascade={"remove"})

does not trigger an automatic database-level ON DELETE CASCADE. The cascade={"remove"} option in Doctrine is designed for entity lifecycle management within the unit of work. It tells Doctrine to delete child entities from its internal tracking list before deleting the parent entity, effectively preventing orphan exception errors during the transaction. It does not instruct the database engine to execute a cascading delete query against the foreign key table.

To achieve true ON DELETE CASCADE at the database level, you must explicitly define it using the orphanRemoval or, more specifically for this requirement, the onDelete option within the mapping. In modern Doctrine versions (2.6+), this is handled directly in the @ORM\ManyToOne annotation or via the fluent interface.

Update your Child.php entity to include the onDelete constraint:

/**
 * @ORM\ManyToOne(targetEntity="Father")
 * @ORM\JoinColumn(name="father_id", referencedColumnName="id", onDelete="CASCADE")
 */
private $father;

Alternatively, if you prefer using the fluent builder pattern or are using newer syntax, you can configure it as:

@ORM\ManyToOne(targetEntity="Father")
@ORM\JoinColumn(
    name="father_id", 
    referencedColumnName="id", 
    onDelete="CASCADE"
)
private $father;

Once this mapping is in place and your schema is updated (using doctrine:schema:update or by running migrations), the database will automatically generate the necessary constraint. You can verify this by inspecting your foreign key definition in your database client; it should explicitly list ON DELETE CASCADE.

It is worth noting a crucial distinction between cascade={"remove"} and onDelete="CASCADE". The former ensures that Doctrine deletes the child entity objects before deleting the parent, which is vital if you are manually managing the entity lifecycle or if the child has its own business logic to be executed on removal. The latter ensures the database engine handles the row deletion immediately upon the foreign key violation or parent deletion.

If you rely solely on cascade={"remove"} without defining onDelete="CASCADE", Doctrine will handle the logical deletion of the entities, but if the underlying database is configured strictly with RESTRICT or NO ACTION (the default), a hard delete of the parent might fail before Doctrine even gets a chance to process the cascade in memory. Explicitly setting onDelete="CASCADE" aligns your application logic with the database constraints, ensuring robustness regardless of the specific transaction flow.

For applications using other frameworks like Laravel, similar concepts apply when managing relationships and migrations, often found in resources like https://laravelcompany.com where they discuss database constraint management. While Doctrine handles this via annotations or metadata drivers, understanding that the mapping must explicitly request the constraint is universal across the PHP ecosystem.

Always ensure your @ORM\JoinColumn defines the correct column names and referential actions. If you are using a custom schema manager or migrations, remember to regenerate them after updating the entity classes so the physical database constraints match your logical mapping.

Our network