Reputation: 361
I'm using the VichUploader to manage File Uploads in my Symfony 3 Application. Now I wonder how to manage deleting Files/ Entities?
Excerpt of the app/config/config.yml:
vich_uploader:
db_driver: orm
mappings:
upload_artists:
uri_prefix: /upload/artists
upload_destination: %kernel.root_dir%/../web/upload/artists
directory_namer: artist_directory_namer
namer: vich_uploader.namer_uniqid
inject_on_load: false
delete_on_update: true
delete_on_remove: true
Excerpt of the Entity:
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @ORM\Table(name="image_file")
* @Vich\Uploadable
*/
class ImageFile {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
*/
private $title;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="upload_artists", fileNameProperty="imageName")
*
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $imageName;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;
/**
* @ORM\ManyToOne(targetEntity="Artist")
* @ORM\JoinColumn(name="artist_id", referencedColumnName="id")
*/
private $artist;
/**
* @ORM\Column(type="boolean")
*/
private $deleted;
Excerpt of the Controller:
namespace Acme\Bundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Acme\Bundle\Entity\Artist;
use Acme\Bundle\Entity\ImageFile;
class ArtistPhotoController extends Controller {
// ...
public function deleteDisabledAction($id = null) {
$artist = $this->getDoctrine()
->getRepository('Bundle:Artist')
->find($id)
;
$repository = $this->getDoctrine()->getRepository('Bundle:ImageFile');
$photosDisabled = $repository->findBy(array('artist' => $artist, 'application' => $this->application, 'deleted' => 1), array('updatedAt' => 'DESC'));
$counter = 0;
foreach ($photosDisabled as $disabled) {
if($disabled->remove()) {
$counter++;
}
}
if ($counter > 0) {
$this->addFlash(
'success',
$counter.' items successfully deleted!'
);
}
}
}
... '$disabled->remove()' was just a test and results in an error message ("undefined method named remove"). How's the correct method to remove/ delete a file/ an entity managed by the VichUploader? Any hints? Thanks in advance!
Upvotes: 1
Views: 4041
Reputation: 601
This is an old question, but this link explain how to implement a Listener to remove entities on the postRemove event of a file, this is an elegant way to manage this :)
Source: https://github.com/dustin10/VichUploaderBundle/issues/523#issuecomment-254858575
Code:
In your services.yml:
app_bundle.listener.removed_file_listener:
class: AppBundle\EventListener\RemovedFileListener
arguments: [@doctrine.orm.entity_manager]
tags:
- { name: kernel.event_listener, event: vich_uploader.post_remove, method: onPostRemove }
And the listener:
namespace AppBundle\EventListener;
use Vich\UploaderBundle\Event\Event;
class RemovedFileListener
{
/**
*
* @param \Doctrine\ORM\EntityManager $em
*/
public function __construct(\Doctrine\ORM\EntityManager $em)
{
$this->em = $em;
}
// make sure a file entity object is removed after the file is deleted
public function onPostRemove(Event $event)
{
// get the file object
$removedFile = $event->getObject();
// remove the file object from the database
$this->em->remove($removedFile);
$this->em->flush();
}
}
With this Listener you can perform any tasks you want, like remove an association between entities for example.
Upvotes: 4
Reputation: 7902
You aren't removing them properly. The basic commands to remove an entity is;
$em = $this->getDoctrine()->getEntityManager();
$em->remove($myEntityObj);
$em->flush();
Upvotes: 3