symfony 4 string instead of UploadedFile in form collection entity with file type create












0















I have the following error: "Argument 1 passed to AppServiceFileUploader::upload() must be an instance of SymfonyComponentHttpFoundationFileUploadedFile, string given"



I applied the solution found in this post but it did not change anything. Quite normal, it's not excactly the same error. Can anyone help me please ?



My goal here is to attach multiple documents to a company from the company screen.



I have been trying to solve the problem since yesterday morning. Now, it's time for me to make a break away from keyboard for 1 or 2 hours ...



Here is the code:



Document entity



namespace AppEntity;

use DoctrineORMMapping as ORM;

/**
* @ORMEntity(repositoryClass="AppRepositoryDocumentRepository")
*/
class Document
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMColumn(type="string", length=255)
*/
private $fichier;

/**
* @var UploadedFile
*/
private $file;

// ...
}


Entreprise entity



namespace AppEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;

/**
* @ORMEntity(repositoryClass="AppRepositoryEntrepriseRepository")
*/
class Entreprise
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMOneToMany(targetEntity="AppEntityDocument", mappedBy="entreprise", orphanRemoval=true, cascade={"persist"})
*/
private $documents;

// ...

public function __construct()
{
$this->documents = new ArrayCollection();
}

// ...

public function getDocuments()
{
return $this->documents;
}

public function addDocument(Document $document)
{
if (!$this->documents->contains($document)) {
$this->documents = $document;
//...
}

return $this;
}

public function removeDocument(Document $document)
{
if ($this->documents->contains($document)) {
$this->documents->removeElement($document);
}

return $this;
}
}


Entreprise Form Typenamespace AppFormType;



use AppEntityEntreprise;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormExtensionCoreTypeIntegerType;
use SymfonyComponentFormExtensionCoreTypeTextareaType;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
use SymfonyComponentFormExtensionCoreTypeFileType;

use SymfonyBridgeDoctrineFormTypeEntityType;

class EntrepriseType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('documents', CollectionType::class, [
'entry_type' => DocumentType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
])
// ...
;
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Entreprise::class,
]);
}
}


Entreprise Controller



namespace AppController;

use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationFileExceptionFileException;
use SymfonyComponentRoutingAnnotationRoute;
use DoctrineCommonPersistenceObjectManager;
use SymfonyComponentHttpFoundationRequest;

use AppEntityEntreprise;
use AppFormTypeEntrepriseType;
use AppRepositoryEntrepriseRepository;
use AppServiceFileUploader;

class EntrepriseController extends AbstractController
{
/**
* @Route("/entreprise/{id}", name="entreprise_detail")
* @Route("/entreprise/new", name="entreprise_new")
*/
public function index(Entreprise $entreprise = null, Request $request, ObjectManager $manager, FileUploader $fileUploader)
{
if (!$entreprise) {
$entreprise = new Entreprise();
}

$formDetail = $this->createForm(EntrepriseType::class, $entreprise);
$formDetail->handleRequest($request);

if ($formDetail->isSubmitted() && $formDetail->isValid()) {
$this->setDefault($entreprise);

// Téléchargement des nouveaux documents rattachés à l'entreprise
$documents = $entreprise->getDocuments();
foreach ($documents as $document) {
if (!$document->getId()){
/** @var SymfonyComponentHttpFoundationFileUploadedFile $file */
$file = $document->getFile();

$document->setFichier($fileUploader->upload($file));
}
}

// Mise à jour de la base de données
$manager->persist($entreprise);
$manager->flush();

return $this->redirectToRoute('entreprise_detail', ['id'=> $entreprise->getId()]);
}

return $this->render('entreprise/index.html.twig', [
'formDetail' => $formDetail->createView(),
'entreprise' => $entreprise,
]);
}
// ...
}


PS : Sorry if my english is not good enough but if you want, you can answer in french.










share|improve this question





























    0















    I have the following error: "Argument 1 passed to AppServiceFileUploader::upload() must be an instance of SymfonyComponentHttpFoundationFileUploadedFile, string given"



    I applied the solution found in this post but it did not change anything. Quite normal, it's not excactly the same error. Can anyone help me please ?



    My goal here is to attach multiple documents to a company from the company screen.



    I have been trying to solve the problem since yesterday morning. Now, it's time for me to make a break away from keyboard for 1 or 2 hours ...



    Here is the code:



    Document entity



    namespace AppEntity;

    use DoctrineORMMapping as ORM;

    /**
    * @ORMEntity(repositoryClass="AppRepositoryDocumentRepository")
    */
    class Document
    {
    /**
    * @ORMId()
    * @ORMGeneratedValue()
    * @ORMColumn(type="integer")
    */
    private $id;

    /**
    * @ORMColumn(type="string", length=255)
    */
    private $fichier;

    /**
    * @var UploadedFile
    */
    private $file;

    // ...
    }


    Entreprise entity



    namespace AppEntity;

    use DoctrineCommonCollectionsArrayCollection;
    use DoctrineORMMapping as ORM;

    /**
    * @ORMEntity(repositoryClass="AppRepositoryEntrepriseRepository")
    */
    class Entreprise
    {
    /**
    * @ORMId()
    * @ORMGeneratedValue()
    * @ORMColumn(type="integer")
    */
    private $id;

    /**
    * @ORMOneToMany(targetEntity="AppEntityDocument", mappedBy="entreprise", orphanRemoval=true, cascade={"persist"})
    */
    private $documents;

    // ...

    public function __construct()
    {
    $this->documents = new ArrayCollection();
    }

    // ...

    public function getDocuments()
    {
    return $this->documents;
    }

    public function addDocument(Document $document)
    {
    if (!$this->documents->contains($document)) {
    $this->documents = $document;
    //...
    }

    return $this;
    }

    public function removeDocument(Document $document)
    {
    if ($this->documents->contains($document)) {
    $this->documents->removeElement($document);
    }

    return $this;
    }
    }


    Entreprise Form Typenamespace AppFormType;



    use AppEntityEntreprise;

    use SymfonyComponentFormAbstractType;
    use SymfonyComponentFormFormBuilderInterface;
    use SymfonyComponentOptionsResolverOptionsResolver;
    use SymfonyComponentFormExtensionCoreTypeTextType;
    use SymfonyComponentFormExtensionCoreTypeIntegerType;
    use SymfonyComponentFormExtensionCoreTypeTextareaType;
    use SymfonyComponentFormExtensionCoreTypeCollectionType;
    use SymfonyComponentFormExtensionCoreTypeFileType;

    use SymfonyBridgeDoctrineFormTypeEntityType;

    class EntrepriseType extends AbstractType
    {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
    ->add('documents', CollectionType::class, [
    'entry_type' => DocumentType::class,
    'entry_options' => ['label' => false],
    'allow_add' => true,
    'by_reference' => false,
    'allow_delete' => true,
    ])
    // ...
    ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
    $resolver->setDefaults([
    'data_class' => Entreprise::class,
    ]);
    }
    }


    Entreprise Controller



    namespace AppController;

    use SymfonyBundleFrameworkBundleControllerAbstractController;
    use SymfonyComponentHttpFoundationFileExceptionFileException;
    use SymfonyComponentRoutingAnnotationRoute;
    use DoctrineCommonPersistenceObjectManager;
    use SymfonyComponentHttpFoundationRequest;

    use AppEntityEntreprise;
    use AppFormTypeEntrepriseType;
    use AppRepositoryEntrepriseRepository;
    use AppServiceFileUploader;

    class EntrepriseController extends AbstractController
    {
    /**
    * @Route("/entreprise/{id}", name="entreprise_detail")
    * @Route("/entreprise/new", name="entreprise_new")
    */
    public function index(Entreprise $entreprise = null, Request $request, ObjectManager $manager, FileUploader $fileUploader)
    {
    if (!$entreprise) {
    $entreprise = new Entreprise();
    }

    $formDetail = $this->createForm(EntrepriseType::class, $entreprise);
    $formDetail->handleRequest($request);

    if ($formDetail->isSubmitted() && $formDetail->isValid()) {
    $this->setDefault($entreprise);

    // Téléchargement des nouveaux documents rattachés à l'entreprise
    $documents = $entreprise->getDocuments();
    foreach ($documents as $document) {
    if (!$document->getId()){
    /** @var SymfonyComponentHttpFoundationFileUploadedFile $file */
    $file = $document->getFile();

    $document->setFichier($fileUploader->upload($file));
    }
    }

    // Mise à jour de la base de données
    $manager->persist($entreprise);
    $manager->flush();

    return $this->redirectToRoute('entreprise_detail', ['id'=> $entreprise->getId()]);
    }

    return $this->render('entreprise/index.html.twig', [
    'formDetail' => $formDetail->createView(),
    'entreprise' => $entreprise,
    ]);
    }
    // ...
    }


    PS : Sorry if my english is not good enough but if you want, you can answer in french.










    share|improve this question



























      0












      0








      0








      I have the following error: "Argument 1 passed to AppServiceFileUploader::upload() must be an instance of SymfonyComponentHttpFoundationFileUploadedFile, string given"



      I applied the solution found in this post but it did not change anything. Quite normal, it's not excactly the same error. Can anyone help me please ?



      My goal here is to attach multiple documents to a company from the company screen.



      I have been trying to solve the problem since yesterday morning. Now, it's time for me to make a break away from keyboard for 1 or 2 hours ...



      Here is the code:



      Document entity



      namespace AppEntity;

      use DoctrineORMMapping as ORM;

      /**
      * @ORMEntity(repositoryClass="AppRepositoryDocumentRepository")
      */
      class Document
      {
      /**
      * @ORMId()
      * @ORMGeneratedValue()
      * @ORMColumn(type="integer")
      */
      private $id;

      /**
      * @ORMColumn(type="string", length=255)
      */
      private $fichier;

      /**
      * @var UploadedFile
      */
      private $file;

      // ...
      }


      Entreprise entity



      namespace AppEntity;

      use DoctrineCommonCollectionsArrayCollection;
      use DoctrineORMMapping as ORM;

      /**
      * @ORMEntity(repositoryClass="AppRepositoryEntrepriseRepository")
      */
      class Entreprise
      {
      /**
      * @ORMId()
      * @ORMGeneratedValue()
      * @ORMColumn(type="integer")
      */
      private $id;

      /**
      * @ORMOneToMany(targetEntity="AppEntityDocument", mappedBy="entreprise", orphanRemoval=true, cascade={"persist"})
      */
      private $documents;

      // ...

      public function __construct()
      {
      $this->documents = new ArrayCollection();
      }

      // ...

      public function getDocuments()
      {
      return $this->documents;
      }

      public function addDocument(Document $document)
      {
      if (!$this->documents->contains($document)) {
      $this->documents = $document;
      //...
      }

      return $this;
      }

      public function removeDocument(Document $document)
      {
      if ($this->documents->contains($document)) {
      $this->documents->removeElement($document);
      }

      return $this;
      }
      }


      Entreprise Form Typenamespace AppFormType;



      use AppEntityEntreprise;

      use SymfonyComponentFormAbstractType;
      use SymfonyComponentFormFormBuilderInterface;
      use SymfonyComponentOptionsResolverOptionsResolver;
      use SymfonyComponentFormExtensionCoreTypeTextType;
      use SymfonyComponentFormExtensionCoreTypeIntegerType;
      use SymfonyComponentFormExtensionCoreTypeTextareaType;
      use SymfonyComponentFormExtensionCoreTypeCollectionType;
      use SymfonyComponentFormExtensionCoreTypeFileType;

      use SymfonyBridgeDoctrineFormTypeEntityType;

      class EntrepriseType extends AbstractType
      {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
      $builder
      ->add('documents', CollectionType::class, [
      'entry_type' => DocumentType::class,
      'entry_options' => ['label' => false],
      'allow_add' => true,
      'by_reference' => false,
      'allow_delete' => true,
      ])
      // ...
      ;
      }

      public function configureOptions(OptionsResolver $resolver)
      {
      $resolver->setDefaults([
      'data_class' => Entreprise::class,
      ]);
      }
      }


      Entreprise Controller



      namespace AppController;

      use SymfonyBundleFrameworkBundleControllerAbstractController;
      use SymfonyComponentHttpFoundationFileExceptionFileException;
      use SymfonyComponentRoutingAnnotationRoute;
      use DoctrineCommonPersistenceObjectManager;
      use SymfonyComponentHttpFoundationRequest;

      use AppEntityEntreprise;
      use AppFormTypeEntrepriseType;
      use AppRepositoryEntrepriseRepository;
      use AppServiceFileUploader;

      class EntrepriseController extends AbstractController
      {
      /**
      * @Route("/entreprise/{id}", name="entreprise_detail")
      * @Route("/entreprise/new", name="entreprise_new")
      */
      public function index(Entreprise $entreprise = null, Request $request, ObjectManager $manager, FileUploader $fileUploader)
      {
      if (!$entreprise) {
      $entreprise = new Entreprise();
      }

      $formDetail = $this->createForm(EntrepriseType::class, $entreprise);
      $formDetail->handleRequest($request);

      if ($formDetail->isSubmitted() && $formDetail->isValid()) {
      $this->setDefault($entreprise);

      // Téléchargement des nouveaux documents rattachés à l'entreprise
      $documents = $entreprise->getDocuments();
      foreach ($documents as $document) {
      if (!$document->getId()){
      /** @var SymfonyComponentHttpFoundationFileUploadedFile $file */
      $file = $document->getFile();

      $document->setFichier($fileUploader->upload($file));
      }
      }

      // Mise à jour de la base de données
      $manager->persist($entreprise);
      $manager->flush();

      return $this->redirectToRoute('entreprise_detail', ['id'=> $entreprise->getId()]);
      }

      return $this->render('entreprise/index.html.twig', [
      'formDetail' => $formDetail->createView(),
      'entreprise' => $entreprise,
      ]);
      }
      // ...
      }


      PS : Sorry if my english is not good enough but if you want, you can answer in french.










      share|improve this question
















      I have the following error: "Argument 1 passed to AppServiceFileUploader::upload() must be an instance of SymfonyComponentHttpFoundationFileUploadedFile, string given"



      I applied the solution found in this post but it did not change anything. Quite normal, it's not excactly the same error. Can anyone help me please ?



      My goal here is to attach multiple documents to a company from the company screen.



      I have been trying to solve the problem since yesterday morning. Now, it's time for me to make a break away from keyboard for 1 or 2 hours ...



      Here is the code:



      Document entity



      namespace AppEntity;

      use DoctrineORMMapping as ORM;

      /**
      * @ORMEntity(repositoryClass="AppRepositoryDocumentRepository")
      */
      class Document
      {
      /**
      * @ORMId()
      * @ORMGeneratedValue()
      * @ORMColumn(type="integer")
      */
      private $id;

      /**
      * @ORMColumn(type="string", length=255)
      */
      private $fichier;

      /**
      * @var UploadedFile
      */
      private $file;

      // ...
      }


      Entreprise entity



      namespace AppEntity;

      use DoctrineCommonCollectionsArrayCollection;
      use DoctrineORMMapping as ORM;

      /**
      * @ORMEntity(repositoryClass="AppRepositoryEntrepriseRepository")
      */
      class Entreprise
      {
      /**
      * @ORMId()
      * @ORMGeneratedValue()
      * @ORMColumn(type="integer")
      */
      private $id;

      /**
      * @ORMOneToMany(targetEntity="AppEntityDocument", mappedBy="entreprise", orphanRemoval=true, cascade={"persist"})
      */
      private $documents;

      // ...

      public function __construct()
      {
      $this->documents = new ArrayCollection();
      }

      // ...

      public function getDocuments()
      {
      return $this->documents;
      }

      public function addDocument(Document $document)
      {
      if (!$this->documents->contains($document)) {
      $this->documents = $document;
      //...
      }

      return $this;
      }

      public function removeDocument(Document $document)
      {
      if ($this->documents->contains($document)) {
      $this->documents->removeElement($document);
      }

      return $this;
      }
      }


      Entreprise Form Typenamespace AppFormType;



      use AppEntityEntreprise;

      use SymfonyComponentFormAbstractType;
      use SymfonyComponentFormFormBuilderInterface;
      use SymfonyComponentOptionsResolverOptionsResolver;
      use SymfonyComponentFormExtensionCoreTypeTextType;
      use SymfonyComponentFormExtensionCoreTypeIntegerType;
      use SymfonyComponentFormExtensionCoreTypeTextareaType;
      use SymfonyComponentFormExtensionCoreTypeCollectionType;
      use SymfonyComponentFormExtensionCoreTypeFileType;

      use SymfonyBridgeDoctrineFormTypeEntityType;

      class EntrepriseType extends AbstractType
      {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
      $builder
      ->add('documents', CollectionType::class, [
      'entry_type' => DocumentType::class,
      'entry_options' => ['label' => false],
      'allow_add' => true,
      'by_reference' => false,
      'allow_delete' => true,
      ])
      // ...
      ;
      }

      public function configureOptions(OptionsResolver $resolver)
      {
      $resolver->setDefaults([
      'data_class' => Entreprise::class,
      ]);
      }
      }


      Entreprise Controller



      namespace AppController;

      use SymfonyBundleFrameworkBundleControllerAbstractController;
      use SymfonyComponentHttpFoundationFileExceptionFileException;
      use SymfonyComponentRoutingAnnotationRoute;
      use DoctrineCommonPersistenceObjectManager;
      use SymfonyComponentHttpFoundationRequest;

      use AppEntityEntreprise;
      use AppFormTypeEntrepriseType;
      use AppRepositoryEntrepriseRepository;
      use AppServiceFileUploader;

      class EntrepriseController extends AbstractController
      {
      /**
      * @Route("/entreprise/{id}", name="entreprise_detail")
      * @Route("/entreprise/new", name="entreprise_new")
      */
      public function index(Entreprise $entreprise = null, Request $request, ObjectManager $manager, FileUploader $fileUploader)
      {
      if (!$entreprise) {
      $entreprise = new Entreprise();
      }

      $formDetail = $this->createForm(EntrepriseType::class, $entreprise);
      $formDetail->handleRequest($request);

      if ($formDetail->isSubmitted() && $formDetail->isValid()) {
      $this->setDefault($entreprise);

      // Téléchargement des nouveaux documents rattachés à l'entreprise
      $documents = $entreprise->getDocuments();
      foreach ($documents as $document) {
      if (!$document->getId()){
      /** @var SymfonyComponentHttpFoundationFileUploadedFile $file */
      $file = $document->getFile();

      $document->setFichier($fileUploader->upload($file));
      }
      }

      // Mise à jour de la base de données
      $manager->persist($entreprise);
      $manager->flush();

      return $this->redirectToRoute('entreprise_detail', ['id'=> $entreprise->getId()]);
      }

      return $this->render('entreprise/index.html.twig', [
      'formDetail' => $formDetail->createView(),
      'entreprise' => $entreprise,
      ]);
      }
      // ...
      }


      PS : Sorry if my english is not good enough but if you want, you can answer in french.







      file-upload symfony4






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 17:25







      Min

















      asked Nov 13 '18 at 14:48









      MinMin

      12




      12
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I had the same issue and solved it by removing the type casting in the getFile() and setFile() in the entity. I suppose this is located in your Document entity.



          Look for:



          public function getFile(): ?string
          {
          return $this->file;
          }

          public function setFile(string $file): self
          {
          $this->file = $file;

          return $this;
          }


          and replace it with



          public function getFile()
          {
          return $this->file;
          }

          public function setFile($file): self
          {
          $this->file = $file;

          return $this;
          }


          This will make sure that the file property will have an Instance of the UploadedFile class instead of invoking the __toString method of the same class (due to casting of type to string).






          share|improve this answer



















          • 1





            Thanks a lot. In fact, that solve many problems !

            – Min
            Jan 6 at 11:26











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283594%2fsymfony-4-string-instead-of-uploadedfile-in-form-collection-entity-with-file-typ%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I had the same issue and solved it by removing the type casting in the getFile() and setFile() in the entity. I suppose this is located in your Document entity.



          Look for:



          public function getFile(): ?string
          {
          return $this->file;
          }

          public function setFile(string $file): self
          {
          $this->file = $file;

          return $this;
          }


          and replace it with



          public function getFile()
          {
          return $this->file;
          }

          public function setFile($file): self
          {
          $this->file = $file;

          return $this;
          }


          This will make sure that the file property will have an Instance of the UploadedFile class instead of invoking the __toString method of the same class (due to casting of type to string).






          share|improve this answer



















          • 1





            Thanks a lot. In fact, that solve many problems !

            – Min
            Jan 6 at 11:26
















          0














          I had the same issue and solved it by removing the type casting in the getFile() and setFile() in the entity. I suppose this is located in your Document entity.



          Look for:



          public function getFile(): ?string
          {
          return $this->file;
          }

          public function setFile(string $file): self
          {
          $this->file = $file;

          return $this;
          }


          and replace it with



          public function getFile()
          {
          return $this->file;
          }

          public function setFile($file): self
          {
          $this->file = $file;

          return $this;
          }


          This will make sure that the file property will have an Instance of the UploadedFile class instead of invoking the __toString method of the same class (due to casting of type to string).






          share|improve this answer



















          • 1





            Thanks a lot. In fact, that solve many problems !

            – Min
            Jan 6 at 11:26














          0












          0








          0







          I had the same issue and solved it by removing the type casting in the getFile() and setFile() in the entity. I suppose this is located in your Document entity.



          Look for:



          public function getFile(): ?string
          {
          return $this->file;
          }

          public function setFile(string $file): self
          {
          $this->file = $file;

          return $this;
          }


          and replace it with



          public function getFile()
          {
          return $this->file;
          }

          public function setFile($file): self
          {
          $this->file = $file;

          return $this;
          }


          This will make sure that the file property will have an Instance of the UploadedFile class instead of invoking the __toString method of the same class (due to casting of type to string).






          share|improve this answer













          I had the same issue and solved it by removing the type casting in the getFile() and setFile() in the entity. I suppose this is located in your Document entity.



          Look for:



          public function getFile(): ?string
          {
          return $this->file;
          }

          public function setFile(string $file): self
          {
          $this->file = $file;

          return $this;
          }


          and replace it with



          public function getFile()
          {
          return $this->file;
          }

          public function setFile($file): self
          {
          $this->file = $file;

          return $this;
          }


          This will make sure that the file property will have an Instance of the UploadedFile class instead of invoking the __toString method of the same class (due to casting of type to string).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 15:00









          Thanasis PapThanasis Pap

          1,54621215




          1,54621215








          • 1





            Thanks a lot. In fact, that solve many problems !

            – Min
            Jan 6 at 11:26














          • 1





            Thanks a lot. In fact, that solve many problems !

            – Min
            Jan 6 at 11:26








          1




          1





          Thanks a lot. In fact, that solve many problems !

          – Min
          Jan 6 at 11:26





          Thanks a lot. In fact, that solve many problems !

          – Min
          Jan 6 at 11:26


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283594%2fsymfony-4-string-instead-of-uploadedfile-in-form-collection-entity-with-file-typ%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()