Skip to content

[CookBook] File upload with Doctrine - error when using PostRemove #2229

Closed
@winzou

Description

@winzou

Hi,

I have an entity named A, which owns a OneToOne relation with the Document entity of the cookbook (the one that is being uploaded), and with cascade={remove}.

If I delete A, then Doctrine deletes Document, and only after deletion it tries to execute its PostRemove callback. So in this callback you can't access any property of Document. If you do, you face an Entity was not found exception.

This is the same case as in #712 where you can't access the id property.

The same workaround should be used: save the filename in PreRemove, and use it from PostRemove. This code actually works:

/**
 * @ORM\PreRemove()
 */
public function preRemoveUpload()
{
  // we temporarly save the filename
  $this->filename = $this->getUploadRootDir().'/'.$this->getMyFileName();
}

/**
 * @ORM\PostRemove()
 */
public function removeUpload()
{
  // we use the saved filename
  if (file_exists($this->filename)) {
    unlink($this->filename);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions