title: Unit of Work Exception Symfony
body: <p>That is very weird, when I try my code in developer and works fine, but, in production give me a 500 error while I try get the results.</p>
  
  <p><strong>var/logs/prod.log</strong>: </p>
  
  <pre><code>request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "Call to a member function setValue() on null" at /my_route/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 2745 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function setValue() on null at /my_route/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2745)"} []
  </code></pre>
  
  <p>Entities implicated:
  <strong>Contract</strong></p>
  
  <pre><code>class Contract
  {
  /**
   * @var integer
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue()
   * @ORM\Id()
   */
  private $id;
  /**
   * @ORM\Column(type="integer", nullable=false)
   */
  private $duration;
  /**
   * @ORM\Column(type="string", nullable=true)
   */
  private $nifHolder;
  
  /**
   * @var Hotel
   * @ORM\OneToOne(targetEntity="AppBundle\Entity\Hotel", inversedBy="contractHotel")
   */
  private $hotel;
  </code></pre>
  
  <p>And <strong>Hotel</strong></p>
  
  <pre><code>class Hotel
  {
      /**
       * @var integer
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue()
       * @ORM\Id()
       */
      private $id;
  
  /**
   * @var string
   * @Assert\Length(
   *      max = 100,
   *      maxMessage = "El nombre no puede tener más de 100 caracteres"
   * )
   * @Assert\NotBlank(message="El nombre es obligatorio")
   * @ORM\Column(type="string", length=100, nullable=false)
   */
  private $name;
  
  /**
   * @var string
   * @Assert\Length(
   *      max = 200,
   *      maxMessage = "La dirección no puede tener más de 200 caracteres"
   * )
   * @Assert\NotBlank(message="La dirección es obligatoria")
   * @ORM\Column(type="string", length=200, nullable=false)
   */
  private $address;
    /**
     * @var Contract
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\Contract", mappedBy="hotel")
    */
  private $contractHotel;
  </code></pre>
  
  <p>The route which I have 500 error code: </p>
  
  <pre><code>/**
       * @Route("/contract_list/{hotel}", name="view_contract")
       * @Security("has_role('ROLE_ADMIN')")
       */
      public function listContractAction(Request $request, Hotel $hotel )
      {
          /** @var EntityManager $em */
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $contract = $em-&gt;createQueryBuilder()
              -&gt;select('c')
              -&gt;from('AppBundle:Contract', 'c')
              -&gt;where('c.hotel =:hotel')
              -&gt;setParameter('hotel', $hotel-&gt;getId())
              -&gt;getQuery()
              -&gt;getResult();
  
          if($contract == []) {
              $contract = new Contract();
  
              $em-&gt;persist($contract);
              $contract-&gt;setDuration(0);
              $contract-&gt;setHaveOpticalFiber(false);
              $contract-&gt;setHaveRadioFibraPTP(false);
              $contract-&gt;setOpticalFiberOperator('Not defined');
              $contract-&gt;setHotel($hotel);
              $em-&gt;flush();
          }
  
          return $this-&gt;render('contract/viewContract.html.twig', [
              'contract' =&gt; array_key_exists(0, $contract) ? $contract[0] : $contract,
              'hotel' =&gt; $hotel
  
          ]);
  </code></pre>
  
  <p>In database all are fine, mapped is validate, even when I do $em->flush(); row are insert</p>
  
  <p>EDIT: Is posible that my error is caused because I use "composer install --optimize-autoload" before add the entities?</p>