Giter VIP home page Giter VIP logo

Comments (2)

garak avatar garak commented on June 16, 2024

I tried on a project of mine and indeed the old file is not removed.
Sadly, I also failed to understand the reason, nor I can currently understand how your proposed listener can work. It's still calling the clean method and so the check is still against the same types, right?

from vichuploaderbundle.

GlucNAc avatar GlucNAc commented on June 16, 2024

Sorry for the late answer.

Actually, the listener I propose does work because when I upload a new image, it will be stored as an UploadedFile in the property $imageFile of my entity Article.

When executing handler->clean() method, it will check if the current $object (Article in my case) has an UploadedFile:

protected function hasUploadedFile(object $obj, PropertyMapping $mapping): bool
{
$file = $mapping->getFile($obj);
return $file instanceof UploadedFile || $file instanceof ReplacingFile;
}

Here, $mapping->getFile($obj); will read the property $imageFile and thus will return true.

Then, when executing the remove method, the script will read the value of $imageName (not $imageFile) because the remove method take the field name to be removed as an argument:

public function clean(object $obj, string $fieldName): void
{
$mapping = $this->getMapping($obj, $fieldName);
// nothing uploaded, do not remove anything
if (!$this->hasUploadedFile($obj, $mapping)) {
return;
}
$this->remove($obj, $fieldName);
}

And I call the clean method like this in my custom listener: $this->handler->clean($event->getObject(), 'imageFile');, so what will be removed is the "old" image.

Finally, the UploadedFile will be converted into a File instance, and $imageName will now have the new file name.

from vichuploaderbundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.