Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony vich上传包-图像和照片在一个实体中-仅文档文件存储在数据库中_Symfony_Vichuploaderbundle - Fatal编程技术网

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

Symfony vich上传包-图像和照片在一个实体中-仅文档文件存储在数据库中,symfony,vichuploaderbundle,Symfony,Vichuploaderbundle,我想上传并存储两个文件:文档和文档封面图像在我的数据库中。但只有文件列。第二个图像不在里面 这就是我的实体类的外观: class Document { /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Co

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

这就是我的实体类的外观:

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;
}
下面是我的二传手的样子:

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
当我查看这些目录时,文件存在于其中,但当我查看数据库时,只有$documentFile。

看来您应该更新数据库架构。您可以使用以下命令:

php app/console doctrine:schema:update --force
但是,如果您有生产环境,那么这不是更新数据库模式的好方法。在这种情况下,您应该创建

此外,我建议按照以下方式实现setDocumentCover设置器,以避免在只更新实体中的一个字段(documentCover)时出现文件保存错误

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

似乎未正确定义
$coverName
字段。应该是这样的(注意docblock是如何声明的):