【问题标题】:Symfony/ VichUploaderBundle: Remove EntitySymfony/ VichUploaderBundle:删除实体
【发布时间】:2016-06-02 13:42:01
【问题描述】:

我正在使用 VichUploader 在我的 Symfony 3 应用程序中管理文件上传。现在我想知道如何管理删除文件/实体?

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

实体摘录:

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;

控制器摘录:

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()' 只是一个测试并导致错误消息(“名为 remove 的未定义方法”)。 删除/删除由 VichUploader 管理的文件/实体的正确方法是什么?有什么提示吗? 提前致谢!

【问题讨论】:

    标签: php symfony vichuploaderbundle


    【解决方案1】:

    这是一个老问题,但是这个链接解释了如何实现一个监听器来删除一个文件的 postRemove 事件上的实体,这是一种优雅的管理方式:)

    来源: https://github.com/dustin10/VichUploaderBundle/issues/523#issuecomment-254858575

    代码:

    在你的 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 }
    

    还有听者:

    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();
        }
    }
    

    使用此侦听器,您可以执行任何您想要的任务,例如删除实体之间的关联。

    【讨论】:

      【解决方案2】:

      您没有正确删除它们。 basic commands to remove an entity 是;

      $em = $this->getDoctrine()->getEntityManager();
      $em->remove($myEntityObj);
      $em->flush();
      

      【讨论】:

      • 非常感谢 - 这解决了我的问题!我是 Symfony 的新手,并没有想到要查看教义而不是 vichuploader 文档。
      猜你喜欢
      • 1970-01-01
      • 2019-06-15
      • 2022-08-24
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多