title: sf2 instance of Doctrine\ORM\PersistentCollection given
body: <p>I'm new in php and symfony
  I use a file service for the upload in symfony 2.8
  I got 2 tables One Salon to many Files</p>
  
  <p>I can upload multiple files in my newaction.
  The problem happened at the editAction of my SalonController.</p>
  
  <p>I would like to remove the older files in order to add newfiles in my editfiles.html.twig. And I tried a foreach in order to get this array...</p>
  
  <p>Could you help me please ? </p>
  
  <p>Here is the error </p>
  
  <blockquote>
    <p>Type error: Argument 1 passed to DefaultBundle\Service\FileService::upload() must be an instance of DefaultBundle\Entity\File, instance of Doctrine\ORM\PersistentCollection given, called in /var/www/html/salon-beaute/src/SalonBundle/Controller/SalonController.php on line 131</p>
  </blockquote>
  
  <p>And the stack trace focused on the line 18 of the fileservice.php</p>
  
  <blockquote>
    <p>public function upload(File $file = null, $type) {</p>
  </blockquote>
  
  <p>Here is the my newaction and editaction of my SalonController</p>
  
  <pre><code>class SalonController extends Controller
  </code></pre>
  
  <p>{
      /**
       * Creates a new salon entity.
       *
       * @Route("/new", name="salon_new")
       * @Method({"GET", "POST"})
       */
      public function newAction(Request $request)
      {
          $this->denyAccessUnlessGranted('ROLE_SALON');
          $em = $this->getDoctrine()->getManager();
          $salon = new Salon();</p>
  
  <pre><code>    $options = array('role' =&gt; $this-&gt;getUser()-&gt;getRoles(), 'page' =&gt; 'add');
  
      $form = $this-&gt;createForm( 'SalonBundle\Form\SalonType',$salon, $options);
      $user = $this-&gt;getUser();
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
  
          $uploadService = $this-&gt;get('app.file');
  
          $myFiles = $form-&gt;get('file')-&gt;getData();
  
          foreach($myFiles['path'] as $new_file) {
  
              $file = new File();
              $file-&gt;setPath($new_file);
              $file-&gt;setSalon($salon);
              $salon-&gt;addFile($file);
              $uploadService-&gt;upload($file, 'salon');
              $em-&gt;persist($file);
          }
  
          $user-&gt;setSalon($salon);
          $salon-&gt;setEnable(false);
          $em-&gt;persist($user);
          $em-&gt;persist($salon);
          $em-&gt;flush();
  
          $this-&gt;get('email')-&gt;salon($salon);
          return $this-&gt;redirectToRoute('salon_show', array('id' =&gt; $salon-&gt;getId()));
      }
  </code></pre>
  
  <blockquote>
  <pre><code>/**
   * Displays a form to edit an existing salon entity.
   *
   * @Route("/edit", name="salon_edit")
   * @Method({"GET", "POST"})
   * @Security("has_role('ROLE_SALON')")
   */
  public function editAction(Request $request)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
      $user = $this-&gt;getUser();
      $salon = $user-&gt;getSalon();
      $deleteForm = $this-&gt;createDeleteForm($salon);
  </code></pre>
  </blockquote>
  
  <pre><code>    $options = array('role' =&gt; $this-&gt;getUser()-&gt;getRoles(), 'page' =&gt; 'edit');
  
      $editForm = $this-&gt;createForm('SalonBundle\Form\SalonType', $salon, $options);
  
      $editForm-&gt;handleRequest($request);
  
      if ($editForm-&gt;isSubmitted() &amp;&amp; $editForm-&gt;isValid()) {
          // If new file
          $edit_file = $editForm-&gt;get('edit_file')-&gt;getData();
          if (!is_null($edit_file)) {
              $file_current = $salon-&gt;getFiles();
              $salon-&gt;getFiles($edit_file);
              if (!$this-&gt;get('app.file')-&gt;upload($salon-&gt;getFiles(), 'salon'))
                  $salon-&gt;getFiles($file_current);
          }
  
          foreach($edit_file['path'] as $new_edit_file) {
              $file = new File();
              $file-&gt;setPath($new_edit_file);
              $file-&gt;setSalon($salon);
              $salon-&gt;addFile($file);
              $this-&gt;get('app.file')-&gt;upload($file);
              $em-&gt;persist($file);
          }
  
          $this-&gt;getDoctrine()-&gt;getManager()-&gt;flush();
          $this-&gt;addFlash('success', 'Le partenaire a bien été modifié');
          return $this-&gt;redirectToRoute('salon_show');
      }
  
      return $this-&gt;render('@Salon/salon/edit.html.twig', array(
          'salon' =&gt; $salon,
          'edit_form' =&gt; $editForm-&gt;createView(),
          'delete_form' =&gt; $deleteForm-&gt;createView(),
      ));
  }
  
      return $this-&gt;render('@Salon/salon/new.html.twig', array(
          'salon' =&gt; $salon,
          'form' =&gt; $form-&gt;createView(),
      ));
  }
  </code></pre>
  
  <p>A portion of my SalonType</p>
  
  <pre><code>    class SalonType extends AbstractType
  {
      /**
       * {@inheritdoc}
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $page = $options['page'];
  
          $builder
              -&gt;add('name', TextType::class, array(
                  'label' =&gt; 'Nom du salon'
              ))
              -&gt;add('content', TextareaType::class, array(
                  'label' =&gt; 'Descriptif'
              ))
              -&gt;add('address_salon', AddressType::class, array(
                  'label' =&gt; ' '
              ))
          ;
  
          if($page == 'add') {
              $builder
                  -&gt;add('file', FileType::class, array(
                      'label' =&gt; 'Vous pouvez téléchargez jusqu\'à 3 images',
                      'required' =&gt; false,
                      'mapped' =&gt;false,
                  ));
          }
  
          if($page == 'edit') {
              $builder
                  -&gt;add('edit_file', FileType::class, array(
                      'label' =&gt; 'Nouveau fichier',
                      'required' =&gt; false,
                      'mapped' =&gt; false
                  ));
          };
  
      }
  
  
  
      /**
       * {@inheritdoc}
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'SalonBundle\Entity\Salon',
              'role' =&gt; null
          ));
          $resolver
              -&gt;setRequired(['page']);
      }
  
  
  My fileType
  
      class FileType extends AbstractType {
  
      public function buildForm(FormBuilderInterface $builder, array $options) {
          $builder
              -&gt;add('path', \Symfony\Component\Form\Extension\Core\Type\FileType::class, array(
                  'label' =&gt; 'Image',
                  'multiple' =&gt; true,
              ))
          ;
      }
  
      public function configureOptions(OptionsResolver $resolver) {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; null,
          ));
      }
  }
  
  My fileService
  
      &lt;?php
  namespace DefaultBundle\Service;
  
  use DefaultBundle\Entity\File;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  
  class FileService {
      private $directory_root;
      private $directory_default;
  
  
      public function __construct($directory_root, $directory_default) {
          $this-&gt;directory_root = $directory_root;
          $this-&gt;directory_default = $directory_default;
      }
  
      public function upload(File $file = null, $type) {
          $path = $this-&gt;getDirectory($type);
  
          if (is_null($file))
              return false;
  
          // Get file (Class UploadFile)
          $file_uploaded = $file-&gt;getPath();
  
          if ($file_uploaded-&gt;getError())
              return false;
  
          // Move file
          $file_name = md5(uniqid()) . '.' . $file_uploaded-&gt;guessExtension();
          $file_uploaded-&gt;move($this-&gt;directory_root . $path, $file_name);
  
          // Update object file
          $path .= '/' . $file_name;
          $file-&gt;update($file_uploaded, $path);
  
          return true;
      }
  
      public function delete(File $file){
          $path = $this-&gt;directory_root.$file-&gt;getPath();
          if(file_exists($path))
              unlink($path);
      }
  
      private function getDirectory($type) {
          switch ($type) {
              default:
                  return $this-&gt;directory_default;
          }
      }
  }
  </code></pre>