PostRemove on an orphanRemoval is not called #5397

Closed
opened 2026-01-22 15:06:23 +01:00 by admin · 4 comments
Owner

Originally created by @johndodev on GitHub (Jan 25, 2017).

Originally assigned to: @lcobucci on GitHub.

Hello, I'm also facing an event problem of orphanRemoval (postDelete).

The simpler example is an user with an avatar one-to-one field, wich is an StoredFile entity.
When I set avatar to null, the StoredFile is removed from the database, but the postRemove seems not executed, and this is where I want to remove the file from the filesystem (is this a bad design ?).

Is this a bug ?

<?php 

class User
{
    /**
     * @ORM\OneToOne(targetEntity="StoredFile", orphanRemoval=true, cascade={"all"})
     */
    private $avatar;
}

class StoredFileListener
{
    public function postRemove(StoredFile $storedFile, LifecycleEventArgs $event)
    {
        try {
            $this->flysystem->delete($storedFile->getPath());
        } catch (FileNotFoundException $e) {
            // it's ok
        }
    }
}

// postRemove is not called
$user->setAvatar(null);
$em->flush();  
Originally created by @johndodev on GitHub (Jan 25, 2017). Originally assigned to: @lcobucci on GitHub. Hello, I'm also facing an event problem of orphanRemoval (postDelete). The simpler example is an user with an avatar one-to-one field, wich is an StoredFile entity. When I set avatar to null, the StoredFile is removed from the database, but the postRemove seems not executed, and this is where I want to remove the file from the filesystem (is this a bad design ?). Is this a bug ? ``` <?php class User { /** * @ORM\OneToOne(targetEntity="StoredFile", orphanRemoval=true, cascade={"all"}) */ private $avatar; } class StoredFileListener { public function postRemove(StoredFile $storedFile, LifecycleEventArgs $event) { try { $this->flysystem->delete($storedFile->getPath()); } catch (FileNotFoundException $e) { // it's ok } } } // postRemove is not called $user->setAvatar(null); $em->flush(); ```
admin added the BugInvalid labels 2026-01-22 15:06:23 +01:00
admin closed this issue 2026-01-22 15:06:23 +01:00
Author
Owner

@Ocramius commented on GitHub (Jan 25, 2017):

(is this a bad design ?).

No, this seems fine (compared to most listener usages :-) )

Is this a bug ?

Can a scenario be created? Look at 9c76c3766f/tests/Doctrine/Tests/ORM/Functional/Ticket

@Ocramius commented on GitHub (Jan 25, 2017): > (is this a bad design ?). No, this seems fine (compared to most listener usages :-) ) > Is this a bug ? Can a scenario be created? Look at https://github.com/doctrine/doctrine2/tree/9c76c3766fb5be1bc78e86b93b3fa4f4c17f0ae4/tests/Doctrine/Tests/ORM/Functional/Ticket
Author
Owner

@johndodev commented on GitHub (Jan 25, 2017):

Is this one ok ?
c28d15a585/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6256Test.php

Should I make a PR ? On which branch (I used 2.5 cause I don't have php7 yet - mailparse needed) ?

So on the test file, the first one is passing, not the second one.

@johndodev commented on GitHub (Jan 25, 2017): Is this one ok ? https://github.com/johndodev/doctrine2/blob/c28d15a5855fed27a838cf201a3cd095e4013f83/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6256Test.php Should I make a PR ? On which branch (I used 2.5 cause I don't have php7 yet - mailparse needed) ? So on the test file, the first one is passing, not the second one.
Author
Owner

@Ocramius commented on GitHub (Jan 25, 2017):

@johndodev please send a PR against master for evaluation, and yes: that's perfect! Travis-CI will test PHP7 for you, no problem :-)

@Ocramius commented on GitHub (Jan 25, 2017): @johndodev please send a PR against `master` for evaluation, and yes: that's perfect! Travis-CI will test PHP7 for you, no problem :-)
Author
Owner

@johndodev commented on GitHub (Mar 16, 2017):

Re,

The test do pass (I made a misstake). So I did some research again on my code and the conclusion that the listener was not called was wrong.

It is called, but the entity passed to the postRemove event is a proxy, with a null ID. So it can't be loaded, but doctrine try to, and give me a "random" entity in the database (always the same, but neither the first one nor the last).

Sooo, where is the error here ? The fact that the proxy is passed in the postRemove listener ? Or the fact that doctrine try to load an entity with a null id (and success to) ? Or something else ?

@johndodev commented on GitHub (Mar 16, 2017): Re, The test do pass (I made a misstake). So I did some research again on my code and the conclusion that the listener was not called was wrong. It is called, but the entity passed to the postRemove event is a proxy, with a null ID. So it can't be loaded, but doctrine try to, and give me a "random" entity in the database (always the same, but neither the first one nor the last). Sooo, where is the error here ? The fact that the proxy is passed in the postRemove listener ? Or the fact that doctrine try to load an entity with a null id (and success to) ? Or something else ?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5397