Cascade Persist & Remove in Doctrine

Yusuf Biberoğlu
2 min readSep 7, 2020

--

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;

— edit #[ORM\OneToMany(mappedBy: ‘cv’, targetEntity: Certificate::class, cascade: [‘persist’, ‘remove’])]

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.

— — —

If you find this useful, please don’t forget to clap 👏 and follow me. I will continue according to your request.

My YouTube Channel;

https://yusufbiberoglu.com

mail@yusufbiberoglu.com

My Udemy Course;

Symfony application using Google OAuth for authentication. When a user signs in through Google, our system will check if the user already exists in our database. If not, it will create a new user account. Once authenticated, either as a new or existing user, our application will generate a JSON Web Token (JWT). Built on the API Platform.

If you want a detailed explanation, please purchase my Udemy course.

https://www.udemy.com/course/google-oauth-php-symfony-api-platform-jwt/?referralCode=67A58BBAE3151CDC1255

Discount Coupon: AD25A625CB8976085C88

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

--

--

Yusuf Biberoğlu
Yusuf Biberoğlu

Written by Yusuf Biberoğlu

Software Developer 💻 (PHP-Symfony, Flutter, Dart, Firebase) https://www.linkedin.com/in/yusufbiberoglu/

Responses (1)