2016-01-27 34 views
0

我想上传和存储文件和文件封面图像在我的数据库中。但是只有文件列。第二张图片不在里面。vich上传包 - 图像和照片在一个实体中 - 仅文件文件存储在数据库中

这是我的实体类的样子:

class Document 
{ 
/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="document_title", type="string", length=255) 
    */ 
    private $documentTitle; 

    /** 
    * @ORM\Column(type="string", length=255) 
    * @var string 
    */ 
    private $fileName; 
    /** 
    *@Vich\UploadableField(mapping="user_documents", fileNameProperty="fileName") 
    * @var File 
    */ 
    private $documentFile; 
    /* 
    * @ORM\Column(type="string", length=255) 
    * @var string 
    */ 
    private $coverName; 
    /** 
    *@Vich\UploadableField(mapping="documents_covers", fileNameProperty="coverName") 
    * @var File 
    */ 
    private $documentCover; 

    /** 
    * @ORM\ManyToOne(targetEntity="Foo\UserBundle\Entity\User", inversedBy="documents") 
    **/ 
    private $owner; 
} 

这里我有多么制定者的样子:

public function setDocumentFile(File $documentFile = null) 
{ 
    $this->documentFile = $documentFile; 
    if ($documentFile){ 
     $this->updatedAt = new \DateTime('now'); 
    } 
    return $this; 
} 

/** 
* @param File $documentCover 
*/ 
public function setDocumentCover(File $documentCover = null) 
{ 
    $this->documentCover = $documentCover; 
} 

而且我VICH上传配置:

vich_uploader: 
    db_driver: orm 
    storage: file_system 
    mappings: 
     documents_covers: 
      uri_prefix: %app.path.documents_covers% 
      upload_destination: %kernel.root_dir%/../web/uploads/images/documents 
      namer: vich_uploader.namer_uniqid 
     user_documents: 
      uri_prefix: %app.path.user_documents% 
      upload_destination: %kernel.root_dir%/../web/uploads/files/user/documents 
      namer: vich_uploader.namer_uniqid 

当我看进入那里的目录,文件存在那里,但是当我在DB看,只有$ documentFile。 Document table data

回答

0

看来你应该更新你的数据库模式。你可以使用这个命令:

php app/console doctrine:schema:update --force 

但是,如果你有生产环境,这不是一个更新数据库模式的好方法。在这种情况下,您应该创建migration

此外,我建议按照以下方法实现您的setDocumentCover setter,以避免在实体中只更新一个字段(documentCover)时发生文件保存错误。

public function setDocumentCover(File $documentCover = null) 
{ 
    $this->documentCover = $documentCover; 
    if ($documentCover){ 
     $this->updatedAt = new \DateTime('now'); 
    } 
    return $this; 
} 
0

看起来像$coverName字段没有正确定义。它应该是这样的(请注意docblock是如何声明的):

/** 
* @ORM\Column(type="string", length=255) 
* @var string 
*/ 
private $coverName; 
相关问题