Cascade Persist & Remove in Doctrine

In this article i will show cascade={"persist", "remove"} operations with examples. It is often used on mappedBy property side of the relationship. But can be use it both sides.

For example, assuming that there is class Cv(Curriculum Vitae) an entity and the OneToMany no cascade option as below;

You just need to call persist() on any new objects after form submitted in your controller as below;

But If you add cascade options to the OneToMany in Cv class;

cascade={"persist"} meaning that persisting entity Cv, doctrine will also persist all Reference and Certificate entities in the Collection as below;

You don’t need to call persist() on any new objects in controller as below;

Also using cascade={“remove”} when you remove an object, all objects in the association have been removed so be careful when using it.

The following cascade options exist:

  • persist : Cascades persist operations to the associated entities.
  • remove : Cascades remove operations to the associated entities.
  • merge : Cascades merge operations to the associated entities.
  • detach : Cascades detach operations to the associated entities.
  • refresh : Cascades refresh operations to the associated entities.
  • all : Cascades persist, remove, merge, refresh and detach operations to associated entities.

Here’s my articles which might interest you;

How Computer Works and What is Binary Number System?

Testing Benford’s Law on Symfony 5, Php Framework

Symfony 5 PhpSpreadsheet (Creating Excel File)

JWT Authentication and Refresh Token to API Platform

Symfony 5 Birthday Reminder Command

Symfony search engine with elasticsearch

Symfony 5 Contact Form with Mailer and Mime component(MailerInterface)

Symfony 5 Confirmation email without FosUserBundle

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store