I'm trying to upload an excel file in a symfony2 form, but without giving any errors, it's not inserting anything in this field. Other fields are filled in well in my database. Anyone has an idea?
I have a table files and a table orders with field delivery_list_id referring to the id of the file. At least, that's the idea.
Here is the entity part:
/**
* @var File
* @ORM\ManyToOne(targetEntity="Myapp\Bundle\CoreBundle\Entity\File", cascade={"persist", "remove"})
* @ORM\JoinColumn(referencedColumnName="id", nullable=true)
*/
private $deliveryList;
/**
* @var UploadedFile
*
* @Assert\File(
* mimeTypes = {"application/vnd.ms-excel", "application/vnd.ms-office", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "text/plain", "text/csv", "application/octet-stream"},
* mimeTypesMessage = "The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}"
* )
*/
private $uploadedDeliveryList;
/**
* @return File
*/
public function getDeliveryList()
{
return $this->deliveryList;
}
/**
* @param File $deliveryList
*/
public function setDeliveryList($deliveryList)
{
$this->deliveryList = $deliveryList;
}
/**
* @return UploadedFile
*/
public function getUploadedDeliveryList()
{
return $this->uploadedDeliveryList;
}
/**
* @param UploadedFile $uploadedDeliveryList
*
* @return $this
*/
public function setUploadedDeliveryList($uploadedDeliveryList)
{
$this->uploadedDeliveryList = $uploadedDeliveryList;
return $this;
}
Here's the twig part:
$builder
->add('uploadedDeliveryList', 'bootstrap_file', array(
'initialCaption' => 'Please select an excelfile or csv file',
'file' => $order->getDeliveryList(),
'initialPreview' => ($order->getDeliveryList() ? '<a type="button" data-toggle="modal" data-target=".preview_' . $order->getDeliveryList()->getId() . '"><img src="' . $this->router->generate('file_thumbnail', ['id' => $order->getDeliveryList()->getId()]) . '" alt="preview"></a>' : ''),
'label' => 'Upload a delivery list',
'attr' => array(
'class' => 'col-sm-12 file',
'accept' => 'application/vnd.ms-excel, application/vnd.ms-office, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, text/plain, text/csv, application/octet-stream'
),
'required' => false,
)
);
My form has
enctype="multipart/form-data"
Anyone has a clue why it's updating everything in my orders table, but not inserting the file in the files table (nor the delivery_list_id in my orders table)?
This is my upload handler, where he writes the file to the server, but doesn't do the database update. Even when the script passes there and no error is given. If i set a die('in');, in my entity setDeliveryList, it seems it's not coming in there.
public function handleUploadedFiles(Event $event)
{
$order = $event->getOrder();
if ($uploadedDeliveryList = $order->getUploadedDeliveryList()) {
$file = $this->transformUploadedFileToFile('/' . $order->getClient()->getId() . '/order/' . $order->getNumber() . '/deliverylist', $uploadedDeliveryList, $order->getDeliveryList());
$file->setClient($order->getClient());
$order->setDeliveryList($file);
}
}
bootstrap_file? I believe you would need to change it to aFile field. If you want to force a file to be uploaded, you may also need to add@Assert\NotBlank