0

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);
    }
}
8
  • what is bootstrap_file? I believe you would need to change it to a File field. If you want to force a file to be uploaded, you may also need to add @Assert\NotBlank Commented Feb 26, 2018 at 15:20
  • Also please show your controller code, as this is where the form would be processed. You can check the Symfony Documentation on How to Upload Files to ensure you are following best-practices Commented Feb 26, 2018 at 15:27
  • @fyrye it's with a bootstrap library, when i change it to just 'file' and change the attributes, i got the same problem Commented Feb 26, 2018 at 15:28
  • @fyrye i updated my question with my controller. Commented Feb 26, 2018 at 15:31
  • Check the rights on the upload directory. Commented Feb 26, 2018 at 15:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.