symfony - vich upload bundle - image and photo in one entity - Documents only file is stored in DB -
i want upload , store both files: document, , document cover image in db. there file column. second image not inside.
this how 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 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 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 there directories, files exist there, when in db, there $documentfile. document table data
it seems should update database schema. can use command:
php app/console doctrine:schema:update --force
but if have production environment not way update database schema. in case should create migration.
also suggest implement setdocumentcover setter following way avoid file saving error in case of updating 1 field (documentcover) in entity.
public function setdocumentcover(file $documentcover = null) { $this->documentcover = $documentcover; if ($documentcover){ $this->updatedat = new \datetime('now'); } return $this; }
Comments
Post a Comment