-
Notifications
You must be signed in to change notification settings - Fork 518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Delete entity when file is deleting #1172
Comments
There's an option for that: see https://github.com/dustin10/VichUploaderBundle/blob/master/docs/usage.md#step-3-configure-the-lifecycle-events-optional-step |
This is for delete file. Not entity. When deleting file using form, the file is deleted, but not the entity row : |
You're righe @bastien70. |
I tried, removing the database record from the listener. But oddly, it removes it well, but it recreates a record right after, with the filename and imageSize in null, it's very weird |
Isn't that removing the entity will also remove the object at the same time? |
I discover the same problem as @bastien70 . No idea why this is happening. Currently I can only disable this setting |
I'm getting the same problem.. listener delete the record and re-create right after |
I also had the same problem on my side but I was able to solve it. Make sure that the entity you want to delete is no longer attached to a parent entity.
|
Could you please provide the code on how to do that? |
I've still the same problem. Is there a solution now ? I've a generic Media entity : #[ORM\Entity(repositoryClass: MediaRepository::class)]
#[ORM\HasLifecycleCallbacks]
#[Vich\Uploadable]
class Media implements \Stringable
{
use PrimaryKeyTrait;
use CreatedAtTrait;
use UpdatedAtTrait;
#[ORM\Column(type: Types::INTEGER, nullable: true)]
private ?int $size = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $mimeType = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $originalName = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $fileName = null;
#[Vich\UploadableField(
mapping: 'media',
fileNameProperty: 'fileName',
size: 'size',
mimeType: 'mimeType',
originalName: 'originalName'
)]
private ?File $file = null;
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
}
public function setFile(File $file = null): self
{
$this->file = $file;
if ($file) {
$this->updatedAt = new \DateTimeImmutable();
}
return $this;
}
public function getFile(): ?File
{
return $this->file;
}
// other getters/setters...
} And I associate it with my entities. By example, in my User entity, I've an avatar field : #[ORM\OneToOne(cascade: ['persist', 'remove'], fetch: 'EAGER')]
private ?Media $avatar = null; In the vich_uploader.yaml : vich_uploader:
db_driver: orm
storage: flysystem
mappings:
media:
upload_destination: 'media.storage'
namer: Vich\UploaderBundle\Naming\SmartUniqueNamer
inject_on_load: true
delete_on_update: true
delete_on_remove: true When clicking inside the checkbox and submitting the form to remove current avatar relation, the file is deleted, the Media entity is truncated, but it creates a new media (with just createdAt and updatedAt values) instead of just deleting by the way the "media" row in database |
Support Question
I want to remove attachment entity when file is deleting but the flush not working.
services.yaml
EventListener
The text was updated successfully, but these errors were encountered: