douzen1880 2016-06-02 13:42
浏览 59
已采纳

Symfony / VichUploaderBundle:删除实体

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!

  • 写回答

2条回答 默认 最新

  • douduandiao1368 2016-06-02 13:48
    关注

    You aren't removing them properly. The basic commands to remove an entity is;

    $em = $this->getDoctrine()->getEntityManager();
    $em->remove($myEntityObj);
    $em->flush();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源