title: Symfony2 persist a ManyToOne with attributes
body: <p>This error appears when I try to persist my entity :</p>
  
  <blockquote>
    <p>Warning: spl_object_hash() expects parameter 1 to be object, integer
    given in
    C:\wamp\www\projet\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php
    line 1601</p>
  </blockquote>
  
  <p>This is my entity Site :</p>
  
  <pre><code>/**
   * Etablissement
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Intranet\RhBundle\Entity\SiteRepository")
   */
  class Site {
   /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
   private $id; /**
    * @ORM\OneToMany(targetEntity="Intranet\RhBundle\Entity\SiteEtablissement",
   mappedBy="site", cascade={"persist", "remove"}, orphanRemoval=true)
    * */
   private $siteEtablissements;
   }
  </code></pre>
  
  <p>This is my entity Etablissement :</p>
  
  <pre><code>/**
  * Etablissement
  *
  * @ORM\Table()
  * @ORM\Entity(repositoryClass="Intranet\RhBundle\Entity\EtablissementRepository")
  */
  class Etablissement {
  
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  }
  </code></pre>
  
  <p>And the entity for the relation :</p>
  
  <pre><code>/**
   * @ORM\Entity(repositoryClass="Intranet\RhBundle\Entity\SiteEtablissementRepository")
   */
  class SiteEtablissement {
  
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;
  
  /**
   * @ORM\ManyToOne(targetEntity="Intranet\RhBundle\Entity\Site", inversedBy="siteEtablissements")
   * @ORM\JoinColumn(name="site_id", referencedColumnName="id")
   */
  private $site;
  
  /**
   * @ORM\ManyToOne(targetEntity="Intranet\RhBundle\Entity\Etablissement")
   * @ORM\JoinColumn(name="etablissement_id", referencedColumnName="id", onDelete="CASCADE")
   */
  private $etablissement;
  }
  </code></pre>
  
  <p>So I don't understand why I have this error, my mapping seems to be ok. I don't have any entity error and I have no error before to persist $site in my controller.</p>
  
  <p>This is my controller :</p>
  
  <pre><code>public function gererEtablissementAction(Request $request, Site $site) {
      $form = $this-&gt;createForm(new GererSiteEtabType(), $site);
      if ($request-&gt;getMethod() == 'POST') {
          $form-&gt;handleRequest($request);
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              foreach ($site-&gt;getSiteEtablissements() as $sitEtab)
                  if (is_null($sitEtab-&gt;getAutor()))
                      $sitEtab-&gt;setAutor($this-&gt;getUser());
              $em-&gt;persist($site);
              $em-&gt;flush();
              $this-&gt;get('session')-&gt;getFlashBag()-&gt;add('success', "Les établissement du site " . $site-&gt;getNom() . " ont été modifiés avec succès !");
              return $this-&gt;redirect($this-&gt;generateUrl('intranet_rh_homepage_employes'));
          } else
              $this-&gt;get('session')-&gt;getFlashBag()-&gt;add('danger', "Erreur de formulaire !");
      }
      return $this-&gt;render('IntranetRhBundle:Site:gererEtablissement.html.twig', array('site' =&gt; $site, 'form' =&gt; $form-&gt;createView()));
  }
  </code></pre>
  
  <p>When I print $site->getSiteEtablissements, there are Site and Etablissement on each side of each object...</p>
  
  <p>This is the build form :</p>
  
  <pre><code>    public function buildForm(FormBuilderInterface $builder, array $options) {
      $builder
              -&gt;add('siteEtablissements', 'collection', array(
                  'type' =&gt; new SiteEtablissementType(),
                  'allow_add' =&gt; true,
                  'allow_delete' =&gt; true,
                  'by_reference' =&gt; false,
                  'required' =&gt; false
              ))
      ;
  }
  </code></pre>
  
  <p>And the subform :</p>
  
  <pre><code> public function buildForm(FormBuilderInterface $builder, array $options) {
      $builder
              -&gt;add('etablissement', 'entity', array(
                  'label' =&gt; 'Etablissement :',
                  'class' =&gt; 'IntranetRhBundle:Etablissement',
                  'property' =&gt; 'nom',
                  'query_builder' =&gt; function(EtablissementRepository $er) {
              // récupère les établissements triés par ordre alpabétique  croissant de nom
              return $er-&gt;createQueryBuilder('e')
                      -&gt;orderBy('e.nom', 'ASC');
          },
                  'attr' =&gt; array('class' =&gt; 'form-control')
              ))
      ;
  }
  </code></pre>