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条)

报告相同问题?

悬赏问题

  • ¥15 准备学习小程序搭建,谁能手把手的教我啊?
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥100 AT89C52单片机C语言调试之后再回答
  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)