2017-09-05 29 views
0

我在我的Symfony2.8應用程序中使用VichUploaderBundle將文件屬性添加到我的實體。請參閱下面的實體代碼。使文件在VichUploaderBundle中不可爲空

namespace WebsiteBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Gedmo\Mapping\Annotation as Gedmo; 
use Symfony\Component\HttpFoundation\File\File as UploadedFile; 
use Symfony\Component\Validator\Constraints as Assert; 
use Vich\UploaderBundle\Mapping\Annotation as Vich; 

/** 
* @ORM\Table(name="website_file") 
* @ORM\Entity(repositoryClass="WebsiteBundle\Repository\FileRepository") 
* @Vich\Uploadable 
*/ 
class File 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * @Assert\Type(type="integer") 
    * @Assert\GreaterThan(value="0") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(type="string", length=255) 
    * @Assert\NotBlank() 
    * @Assert\Type(type="string") 
    * @Assert\Length(max="255") 
    */ 
    private $title; 

    /** 
    * @var string 
    * 
    * @ORM\Column(type="text") 
    * @Assert\NotBlank() 
    * @Assert\Type(type="string") 
    */ 
    private $description; 

    /** 
    * @var UploadedFile 
    * 
    * @Vich\UploadableField(mapping="file", fileNameProperty="filename") 
    */ 
    private $file; 

    /** 
    * @var string 
    * 
    * @ORM\Column(type="string", length=255) 
    * @Assert\Type(type="string") 
    * @Assert\Length(max="255") 
    */ 
    private $filename; 

    /** 
    * @var int 
    * 
    * @ORM\Column(type="integer") 
    * @Assert\NotNull() 
    * @Assert\Type(type="integer") 
    * @Assert\GreaterThanOrEqual(value="0") 
    */ 
    private $dummy = 0; 

    /** 
    * @var boolean 
    * 
    * @ORM\Column(type="boolean") 
    * @Assert\NotNull() 
    * @Assert\Type(type="boolean") 
    */ 
    private $published; 

    /** 
    * @var User 
    * 
    * @Gedmo\Blameable(on="create") 
    * @ORM\ManyToOne(targetEntity="WebsiteBundle\Entity\User") 
    * @ORM\JoinColumn(onDelete="CASCADE") 
    * @Assert\Type(type="WebsiteBundle\Entity\User") 
    * @Assert\Valid() 
    */ 
    private $createdBy; 

    /** 
    * @var \DateTime 
    * 
    * @Gedmo\Timestampable(on="create") 
    * @ORM\Column(type="datetime") 
    * @Assert\DateTime() 
    */ 
    private $createdAt; 

    /** 
    * @var User 
    * 
    * @Gedmo\Blameable(on="update") 
    * @ORM\ManyToOne(targetEntity="WebsiteBundle\Entity\User") 
    * @ORM\JoinColumn(onDelete="CASCADE") 
    * @Assert\Type(type="WebsiteBundle\Entity\User") 
    * @Assert\Valid() 
    */ 
    private $updatedBy; 

    /** 
    * @var \DateTime 
    * 
    * @Gedmo\Timestampable(on="update") 
    * @ORM\Column(type="datetime") 
    * @Assert\DateTime() 
    */ 
    private $updatedAt; 

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

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

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

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

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

    /** 
    * @return UploadedFile 
    */ 
    public function getFile() 
    { 
     return $this->file; 
    } 

    /** 
    * @param UploadedFile $file 
    */ 
    public function setFile($file) 
    { 
     $this->file = $file; 
     $this->dummy++; 
    } 

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

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

    /** 
    * @return boolean 
    */ 
    public function getPublished() 
    { 
     return $this->published; 
    } 

    /** 
    * @param boolean $published 
    */ 
    public function setPublished($published) 
    { 
     $this->published = $published; 
    } 

    /** 
    * @return User 
    */ 
    public function getCreatedBy() 
    { 
     return $this->createdBy; 
    } 

    /** 
    * @param User $createdBy 
    */ 
    public function setCreatedBy($createdBy) 
    { 
     $this->createdBy = $createdBy; 
    } 

    /** 
    * @return \DateTime 
    */ 
    public function getCreatedAt() 
    { 
     return $this->createdAt; 
    } 

    /** 
    * @param \DateTime $createdAt 
    */ 
    public function setCreatedAt($createdAt) 
    { 
     $this->createdAt = $createdAt; 
    } 

    /** 
    * @return User 
    */ 
    public function getUpdatedBy() 
    { 
     return $this->updatedBy; 
    } 

    /** 
    * @param User $updatedBy 
    */ 
    public function setUpdatedBy($updatedBy) 
    { 
     $this->updatedBy = $updatedBy; 
    } 

    /** 
    * @return \DateTime 
    */ 
    public function getUpdatedAt() 
    { 
     return $this->updatedAt; 
    } 

    /** 
    * @param \DateTime $updatedAt 
    */ 
    public function setUpdatedAt($updatedAt) 
    { 
     $this->updatedAt = $updatedAt; 
    } 
} 

一切正常,只有一個例外。我想讓文件名不能爲空。文件必須上傳到創建實體,並且在更新期間不能刪除。它只能被改變。一些文件總是必須上傳到實體。如何實現這一目標?如果我添加斷言像文件名:

* @Assert\NotNull() 

然後,它不工作,因爲在驗證表單,文件名是空的。它在持久實體中生成。但是,如果我省略了這個斷言,那麼可以在沒有上傳文件的情況下堅持實體。

+0

我從來沒有使用過這個包, – sh4

回答

1

解決方案非常簡單而又緊密。我只需要將自定義驗證器添加到我的File實體。我是如何做到的?

首先,我將註釋添加到文件實體。請參閱下面的代碼

/** 
* @ORM\Table(name="website_file") 
* @ORM\Entity(repositoryClass="WebsiteBundle\Repository\FileRepository") 
* @Vich\Uploadable 
* @Assert\Callback({"WebsiteBundle\Validator\Entities\FileValidator", "validate"}) 
*/ 
class File 

屬性的註釋保持不變。

/** 
* @var UploadedFile 
* 
* @Vich\UploadableField(mapping="file", fileNameProperty="filename") 
* @Assert\Type(type="Symfony\Component\HttpFoundation\File\UploadedFile") 
*/ 
private $file; 

/** 
* @var string 
* 
* @ORM\Column(type="string", length=255) 
* @Assert\Type(type="string") 
* @Assert\Length(max="255") 
*/ 
private $filename; 

最後一步 - 驗證器,它非常簡單。

class FileValidator 
{ 
    /** 
    * @param File $file 
    * @param ExecutionContextInterface $context 
    */ 
    public static function validate(File $file, ExecutionContextInterface $context) 
    { 
     if ($file->getFilename() === null && $file->getFile() === null) { 
      $context->buildViolation('File cannot be empty.') 
       ->atPath('file') 
       ->addViolation(); 
     } 
    } 
} 

感謝這個文件必須上傳實體,它不能被刪除在更新過程中。

相關問題