title: Symfony Form with Doctrine Entity - Relation not loading for editing
body: <p>I'm having an issue with something I think is probably fairly basic but the cause is escaping me.</p>
  
  <p>I have a two entities, an Organisation and a Subscription. An organisation is linked with one subscription. </p>
  
  <p>I want to be able to edit this via a form (creating via form already works). When I do the following:</p>
  
  <pre><code>    $organisation = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle\Entity\Organisation')
          -&gt;findOneBy(array(
              'id' =&gt; $id
          ));
  
      $form = $this-&gt;createForm(new OrganisationType(), $organisation, array(
          'method' =&gt; 'POST'
      ));
  
      $form-&gt;submit($paramFetcher-&gt;all());
  </code></pre>
  
  <p>I get an exception because the subscription property of the organisation entity is not set (despite passing one which has a subscription into the form defaults). The exception is as follows:</p>
  
  <pre><code>Expected argument of type "AppBundle\Entity\Subscription", "NULL" given
  </code></pre>
  
  <p>This is obviously being thrown by the setSubscription method on the Organisation entity but I'm not sure why as the form should be converting the int value passed to a Subscription entity automatically.</p>
  
  <p>Am I doing something stupid here? I have attached the relevant code below. Thank you!</p>
  
  <p><strong>The simplest controller action that replicates this issue</strong></p>
  
  <pre><code>public function testAction(Request $request)
  {
  
      $organisation = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle\Entity\Organisation')
          -&gt;findOneBy(array('id' =&gt; 7));
  
      $form = $this-&gt;createForm(new OrganisationType(), $organisation);
  
      $form-&gt;submit($request-&gt;request-&gt;all());
  
      return $this-&gt;render('test.html.twig', array(
          'testform' =&gt; $form-&gt;createView()
      ));
  
  }
  </code></pre>
  
  <p><strong>Organisation.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Entity;
  
  use Doctrine\Common\Collections\Collection;
  use Doctrine\ORM\Mapping as ORM;
  use JMS\Serializer\Annotation as JMS;
  use Gedmo\Mapping\Annotation as Gedmo;
  use Symfony\Component\Validator\Constraints as Assert;
  
  /**
   * Organisation
   *
   * @ORM\Table(name="organisation")
   * @ORM\Entity(repositoryClass="AppBundle\Repository\OrganisationRepository")
   */
  class Organisation
  {
  
  const ENABLED = 1;
  const DISABLED = 2;
  
  /**
   * @var int
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   * @JMS\Groups({"default", "list-organisation"})
   */
  private $id;
  
  /**
   * @var string
   *
   * @ORM\Column(name="name", type="string", length=255)
   * @JMS\Groups({"default", "list-organisation"})
   */
  private $name;
  
  /**
   * @var Subscription|null The subscription this organisation is currently linked to
   * @ORM\ManyToOne(targetEntity="Subscription", inversedBy="organisations")
   * @ORM\JoinColumn(name="subscription_id", referencedColumnName="id", onDelete="set null")
   * @JMS\Groups({"default", "list-organisation"})
   */
  private $subscription;
  
  /**
   * @var Collection
   * @ORM\OneToMany(targetEntity="User", mappedBy="organisation")
   * @JMS\Groups({})
   */
  private $users;
  
  /**
   * @var Collection
   * @ORM\OneToMany(targetEntity="Subgroup", mappedBy="organisation")
   * @JMS\Groups({"default"})
   */
  private $subgroups;
  
  /**
   * @var string $enabled
   *
   * @ORM\Column(type="string")
   */
  private $enabled = self::ENABLED;
  
  /**
   * @var datetime $created
   *
   * @Gedmo\Timestampable(on="create")
   * @ORM\Column(type="datetime")
   * @JMS\Groups({"default", "list-organisation"})
   */
  private $created;
  
  /**
   * @var datetime $updated
   *
   * @Gedmo\Timestampable(on="update")
   * @ORM\Column(type="datetime")
   * @JMS\Groups({"default", "list-organisation"})
   */
  private $updated;
  
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set name
   *
   * @param string $name
   * @return Organisation
   */
  public function setName($name)
  {
      $this-&gt;name = $name;
  
      return $this;
  }
  
  /**
   * Sets subscription
   *
   * @param Subscription $subscription
   * @return $this
   */
  public function setSubscription(Subscription $subscription)
  {
      $this-&gt;subscription = $subscription;
  
      return $this;
  }
  
  /**
   * Gets subscription
   *
   * @return Subscription|null
   */
  public function getSubscription()
  {
      return $this-&gt;subscription;
  }
  
  /**
   * Sets enabled
   *
   * @param $enabled
   */
  public function setEnabled($enabled)
  {
      if (!in_array($enabled, array(self::ENABLED, self::DISABLED))) {
          throw new \InvalidArgumentException('Invalid enabled status');
      }
  
      $this-&gt;enabled = $enabled;
  }
  
  /**
   * Gets enabled
   *
   * @return string
   */
  public function getEnabled()
  {
      return $this-&gt;enabled;
  }
  
  /**
   * Get name
   *
   * @return string 
   */
  public function getName()
  {
      return $this-&gt;name;
  }
  
  /**
   * Returns Users belonging to this organisation
   *
   * @return Collection
   */
  public function getUsers()
  {
      return $this-&gt;users;
  }
  
  public function __toString()
  {
      return $this-&gt;getName();
  }
  }
  </code></pre>
  
  <p><strong>Subscription.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Entity;
  
  use Doctrine\Common\Collections\Collection;
  use Doctrine\ORM\Mapping as ORM;
  use JMS\Serializer\Annotation as JMS;
  use Gedmo\Mapping\Annotation as Gedmo;
  
  /**
   * Organisation
   *
   * @ORM\Table(name="subscription")
   * @ORM\Entity(repositoryClass="AppBundle\Repository\SubscriptionRepository")
   */
  class Subscription
  {
  /**
   * @var int
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   * @JMS\Groups({"default", "list-organisation"})
   */
  private $id;
  
  /**
   * @var string
   *
   * @ORM\Column(name="name", type="string", length=255)
   * @JMS\Groups({"default", "list-organisation"})
   */
  private $name;
  
  /**
   * @var Collection
   * @ORM\OneToMany(targetEntity="Organisation", mappedBy="subscription")
   * @JMS\Groups({"default"})
   */
  private $organisations;
  
  /**
   * @var datetime $created
   *
   * @Gedmo\Timestampable(on="create")
   * @ORM\Column(type="datetime")
   * @JMS\Groups({"default"})
   */
  private $created;
  
  /**
   * @var datetime $updated
   *
   * @Gedmo\Timestampable(on="update")
   * @ORM\Column(type="datetime")
   * @JMS\Groups({"default"})
   */
  private $updated;
  
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set name
   *
   * @param string $name
   * @return Subscription
   */
  public function setName($name)
  {
      $this-&gt;name = $name;
  
      return $this;
  }
  
  /**
   * Get name
   *
   * @return string 
   */
  public function getName()
  {
      return $this-&gt;name;
  }
  
  /**
   * Returns Organisations with this subscription type
   *
   * @return Collection
   */
  public function getOrganisations()
  {
      return $this-&gt;organisations;
  }
  
  /**
   * @return string
   */
  public function __toString()
  {
      return $this-&gt;getName();
  }
  }
  </code></pre>
  
  <p><strong>OrganisationType.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Form;
  
  use Doctrine\Common\Persistence\ObjectManager;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  class OrganisationType extends AbstractType
  {
  private $manager;
  
  public function __construct(ObjectManager $objectManager)
  {
      $this-&gt;manager = $objectManager;
  }
  
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder-&gt;add('name');
      $builder-&gt;add('subscription', EntityType::class, array(
          'class' =&gt; 'AppBundle\Entity\Subscription'
      ));
  
  }
  
  public function configureOptions(OptionsResolver $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'AppBundle\Entity\Organisation',
          'csrf_protection' =&gt; false
      ));
  }
  }
  </code></pre>