vich上传包 - 图像和照片在一个实体中 - 仅文档文件存储在DB中

时间:2016-01-27 16:23:50

标签: symfony vichuploaderbundle

我想在我的数据库中上传和存储两个文件:文档和文档封面图像。但是只有文件列。第二张图片不在里面。

这是我的实体类的样子:

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 uploader配置:

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

当我查看目录时,那里存在文件,但是当我查看数据库时,只有$ documentFile。 Document table data

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

看起来$coverName字段未正确定义。它应该是这样的(注意docblock的声明方式):

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