首页 文章

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

提问于
浏览
0

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

这就是我的实体类的样子:

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

    您似乎应该更新数据库架构 . 您可以使用此命令:

    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;
    

相关问题