em->flush($entity) for cascading associations behaves inconsistently between "inserts" and "updates" #6209

Closed
opened 2026-01-22 15:28:58 +01:00 by admin · 2 comments
Owner

Originally created by @TiMESPLiNTER on GitHub (Mar 27, 2019).

Originally assigned to: @Ocramius on GitHub.

Bug Report

Q A
BC Break no
Version 2.6.3

Summary

The flush method for associations behaves inconsistently between "inserts" and "updates" if you provide an entity as an argument.

I have a one-to-many relation with cascade=persist,remove. While the relations' entity gets inserted together with the main entity when I call...

$em->flush($mainEntity)

...the update case ignores changes for the relations' entity and does only persist the changes in the main entity.

Current behavior

Currently only create operations persist cascading relations' entities update operations don't.

How to reproduce

Create a relation:

    $metadata->mapOneToMany([
        'fieldName' => 'translations',
        'targetEntity' => Profile::class,
        'mappedBy' => 'profile',
        'cascade' => ['persist', 'remove'],
    ]);

Insert (works as expected):

$translation = new Translation();
$translation->setLanguage('en');

$profile = new Profile();
$profile->setName('Foo');
$profile->addTranslation($translation);

$em->persist($profile);
$em->flush($profile);

The translation gets persisted together with the profile:

Update (works not as expected):

$profile = $profileRepository->getProfileByName('Foo');
$profile->setName('Bar');
$translation = $profile->getTranslation('en');
$translation->setLanguage('de');

$em->flush($profile);

Only the changes for the profile entity get persisted the changes for the translation entity are ignored.

Expected behavior

I expect that both cases behave the same. Either both ignore sub entities if I only flush a specific parent entity or both do cascade and also store the relations' entity.

Originally created by @TiMESPLiNTER on GitHub (Mar 27, 2019). Originally assigned to: @Ocramius on GitHub. ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | no | Version | 2.6.3 #### Summary The `flush` method for associations behaves inconsistently between "inserts" and "updates" if you provide an entity as an argument. I have a `one-to-many` relation with `cascade=persist,remove`. While the relations' entity gets inserted together with the main entity when I call... `$em->flush($mainEntity)` ...the update case ignores changes for the relations' entity and does only persist the changes in the main entity. #### Current behavior <!-- What is the current (buggy) behavior? --> Currently only create operations persist cascading relations' entities update operations don't. #### How to reproduce <!-- Provide steps to reproduce the bug. If possible, also add a code snippet with relevant configuration, entity mappings, DQL etc. Adding a failing Unit or Functional Test would help us a lot - you can submit one in a Pull Request separately, referencing this bug report. --> Create a relation: ```php $metadata->mapOneToMany([ 'fieldName' => 'translations', 'targetEntity' => Profile::class, 'mappedBy' => 'profile', 'cascade' => ['persist', 'remove'], ]); ``` Insert (works as expected): ```php $translation = new Translation(); $translation->setLanguage('en'); $profile = new Profile(); $profile->setName('Foo'); $profile->addTranslation($translation); $em->persist($profile); $em->flush($profile); ``` The translation gets persisted together with the profile: Update (works **not** as expected): ```php $profile = $profileRepository->getProfileByName('Foo'); $profile->setName('Bar'); $translation = $profile->getTranslation('en'); $translation->setLanguage('de'); $em->flush($profile); ``` Only the changes for the profile entity get persisted the changes for the translation entity are ignored. #### Expected behavior I expect that both cases behave the same. Either both ignore sub entities if I only flush a specific parent entity or both do cascade and also store the relations' entity.
admin added the BugWon't Fix labels 2026-01-22 15:28:58 +01:00
admin closed this issue 2026-01-22 15:28:58 +01:00
Author
Owner

@SenseException commented on GitHub (Mar 27, 2019):

The argument in flush() will be dropped and was already removed in master.

The 2.7 branch still has the argument, but I see that it hasn't a deprecation yet. We probably need one there.

@SenseException commented on GitHub (Mar 27, 2019): The argument in `flush()` will be dropped and was already removed in master. The 2.7 branch still has the argument, but I see that it hasn't a deprecation yet. We probably need one there.
Author
Owner

@Ocramius commented on GitHub (Mar 27, 2019):

I'm gonna close as won't fix this one for now though: flush($entity) will not be worked on.

@Ocramius commented on GitHub (Mar 27, 2019): I'm gonna close as `won't fix` this one for now though: `flush($entity)` will not be worked on.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6209