0

I want to upload and store both files: document, and document cover image in my DB. But there is only file column. Second image is not inside.

This is how my entity class looks like:

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;
}

Here how my setters looks like:

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;
}

And my vich uploader config:

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

When i look into there directories, files exist there, but when i look in DB, there is only $documentFile. Document table data

Krzysztof Koch
  • 59
  • 1
  • 11

2 Answers2

0

It seems you should update your database schema. You can use this command:

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

But if you have production environment this is not a good way to update your database schema. In this case you should create migration.

Also I suggest to implement your setDocumentCover setter following way to avoid file saving error in case of updating only one field (documentCover) in your entity.

public function setDocumentCover(File $documentCover = null)
{
    $this->documentCover = $documentCover;
    if ($documentCover){
        $this->updatedAt = new \DateTime('now');
    }
    return $this;
}
Mikhail Prosalov
  • 4,155
  • 4
  • 29
  • 41
0

Looks like the $coverName field isn't correctly defined. It should be like this (notice how the docblock is declared):

/**
 * @ORM\Column(type="string", length=255)
 * @var string
 */
private $coverName;
K-Phoen
  • 1,260
  • 9
  • 18