2015-10-18 42 views
1

嗨,大家好,我已经实现vichuploadbundle上传图像,但我想在管理员用户删除图像。Vich上传包删除图像

<?php 

namespace George\PageBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use George\UserBundle\Entity\User; 
use Gedmo\Mapping\Annotation as Gedmo; 
use Symfony\Component\HttpFoundation\File\File; 
use Vich\UploaderBundle\Mapping\Annotation as Vich; 

/** 
* Page 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="George\PageBundle\Entity\PageRepository") 
* @Vich\Uploadable 
*/ 
class Page 
{ 
/** 
* @var integer 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @var string 
* 
* @ORM\Column(name="title", type="text") 
*/ 
private $title; 

/** 
* @var string 
* 
* @ORM\Column(name="content", type="text") 
*/ 
private $content; 

/** 
* @var boolean 
* 
* @ORM\Column(name="visible", type="boolean") 
*/ 
private $visible; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="created", type="datetime") 
*/ 
private $created; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="modefied", type="datetime") 
*/ 
private $modefied; 

/** 
* @var string 
* 
* @ORM\Column(name="description", type="string", length=255) 
*/ 
private $description; 

/** 
* @var string 
* 
* @ORM\Column(name="keywords", type="string", length=255) 
*/ 
private $keywords; 

/** 
* @ORM\ManyToOne(targetEntity="George\UserBundle\Entity\User", inversedBy="pages") 
* @ORM\JoinColumn(onDelete="CASCADE") 
*/ 
private $owner; 

//@ORM\Column(length=128, unique=true) 
/** 
* @Gedmo\Slug(fields={"title"}) 
* @ORM\Column(length=128, unique=true) 
*/ 
private $slug; 

/** 
* Get id 
* 
* @return integer 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* Set title 
* 
* @param string $title 
* 
* @return Page 
*/ 
public function setTitle($title) 
{ 
    $this->title = $title; 

    return $this; 
} 

/** 
* Get title 
* 
* @return string 
*/ 
public function getTitle() 
{ 
    return $this->title; 
} 

/** 
* Set content 
* 
* @param string $content 
* 
* @return Page 
*/ 
public function setContent($content) 
{ 
    $this->content = $content; 

    return $this; 
} 

/** 
* Get content 
* 
* @return string 
*/ 
public function getContent() 
{ 
    return $this->content; 
} 

/** 
* Set visible 
* 
* @param boolean $visible 
* 
* @return Page 
*/ 
public function setVisible($visible) 
{ 
    $this->visible = $visible; 

    return $this; 
} 

/** 
* Get visible 
* 
* @return boolean 
*/ 
public function getVisible() 
{ 
    return $this->visible; 
} 

/** 
* Set created 
* 
* @param \DateTime $created 
* 
* @return Page 
*/ 
public function setCreated($created) 
{ 
    $this->created = $created; 

    return $this; 
} 

/** 
* Get created 
* 
* @return \DateTime 
*/ 
public function getCreated() 
{ 
    return $this->created; 
} 

/** 
* Set modefied 
* 
* @param \DateTime $modefied 
* 
* @return Page 
*/ 
public function setModefied($modefied) 
{ 
    $this->modefied = $modefied; 

    return $this; 
} 

/** 
* Get modefied 
* 
* @return \DateTime 
*/ 
public function getModefied() 
{ 
    return $this->modefied; 
} 

/** 
* Set description 
* 
* @param string $description 
* 
* @return Page 
*/ 
public function setDescription($description) 
{ 
    $this->description = $description; 

    return $this; 
} 

/** 
* Get description 
* 
* @return string 
*/ 
public function getDescription() 
{ 
    return $this->description; 
} 

/** 
* Set keywords 
* 
* @param string $keywords 
* 
* @return Page 
*/ 
public function setKeywords($keywords) 
{ 
    $this->keywords = $keywords; 

    return $this; 
} 

/** 
* Get keywords 
* 
* @return string 
*/ 
public function getKeywords() 
{ 
    return $this->keywords; 
} 

/** 
* @return mixed 
*/ 
public function getOwner() 
{ 
    return $this->owner; 
} 

/** 
* @param mixed $owner 
*/ 
public function setOwner(User $owner) 
{ 
    $this->owner = $owner; 
} 

public function getSlug() 
{ 
    return $this->slug; 
} 

// ..... other fields 


/** 
* NOTE: This is not a mapped field of entity metadata, just a simple property. 
* 
* @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName") 
* 
* @var File 
*/ 
private $imageFile; 

/** 
* @ORM\Column(type="string", length=255) 
* 
* @var string 
*/ 
private $imageName; 

/** 
* @ORM\Column(type="datetime") 
* 
* @var \DateTime 
*/ 
private $updatedAt; 

/** 
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance 
* of 'UploadedFile' is injected into this setter to trigger the update. If this 
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter 
* must be able to accept an instance of 'File' as the bundle will inject one here 
* during Doctrine hydration. 
* 
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image 
*/ 
public function setImageFile(File $image = null) 
{ 
    $this->imageFile = $image; 

    if ($image) { 
     // It is required that at least one field changes if you are using doctrine 
     // otherwise the event listeners won't be called and the file is lost 
     $this->updatedAt = new \DateTime('now'); 
    } 
} 

/** 
* @return File 
*/ 
public function getImageFile() 
{ 
    return $this->imageFile; 
} 

/** 
* @param string $imageName 
*/ 
public function setImageName($imageName) 
{ 
    $this->imageName = $imageName; 
} 

/** 
* @return string 
*/ 
public function getImageName() 
{ 
    return $this->imageName; 
} 
} 

你能告诉我什么是最好的解决方案。我认为这将是最好的,如果我有一个方法删除图像,并在该方法尝试使用捆绑生成功能删除图像(我没有找到任何地方),并在数据库中更新它。在前端,这可以不通过ajax或提交...是否有一个在vichuploadbundle文件中删除功能的构建?

回答

1

我发现如何删除图像。该软件包提供了自定义表单类型,以便于上传,删除和下载图像。

当您生成表格,您指定的窗口小部件:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
// ... 

$builder->add('image', 'vich_image', array(
    'required'  => false, 
    'allow_delete' => true, // not mandatory, default is true 
    'download_link' => true, // not mandatory, default is true 
)); 
} 

这增加树枝模板删除IMG :)

相关问题