I've been trying for days to integrate VichUploaderBundle into a symfony 3.4 project with mySql. I followed the documentation of link but I have a problem that I could not solve.
The error occurs in the flush () when creating a new record.
SQLSTATE [23000]: Integrity constraint violation: 1048 Column 'image_name' can not be null
It does not assign any value to the fields imageName, imageSize, updatedAt.
An exception occurred while executing 'INSERT INTO box (date, user, amount, type, cajaconcepto_id, moneda_id, nula, obs, hab, doc, pax, time, image_name, image_size, updated_at) VALUES (?,?,? ,?,?,?,?,?,?,?,?,?,?,?,?,?) 'with params ["2018-03-30", {}, 2, "2", {}, { }, 0, "2", null, null, null, "19:13:56", null, null, null]:
My code is as follows:
/app/config/config.yml
vich_uploader:
db_driver: orm # or mongodb or propel or phpcr
mappings:
product_image:
uri_prefix: /pdf/adjuntos
upload_destination: '%kernel.project_dir%/public/pdf/adjuntos
/src/CajaBundle/Entity/Caja.php ... ...
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName", size="imageSize")
*
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $imageName;
/**
* @ORM\Column(type="integer")
*
* @var integer
*/
private $imageSize;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*/
public function setImageFile(?File $image = null): void
{
$this->imageFile = $image;
if (null !== $image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageName(?string $imageName): void
{
$this->imageName = $imageName;
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function setImageSize(?int $imageSize): void
{
$this->imageSize = $imageSize;
}
public function getImageSize(): ?int
{
return $this->imageSize;
}
...
...
/src/CajaBundle/Controller/DefaultController.php
...
...
$form = $this->createFormBuilder($caja)
->add('imageFile', VichImageType::class, [
'required' => false,
'allow_delete' => true,
'download_label' => '...',
'download_uri' => true,
'download_label' => 'download_file',
'image_uri' => true,
'imagine_pattern' => '...',
])
...
...
/src/CajaBundle/Resources/views/Default/egreso.html.twig
...
...
<div class="form-group">
<label>Adjunto:</label>
{{ form_errors(form.imageFile) }}
{{ form_widget(form.imageFile, {'required': false}) }}
</div>
...
...
Any orientation will be welcome. Thanks in advance