title: symfony2: How to setting the value of field, outside the form
body: <p>I have two tables:</p>
  
  <p><a href="https://i.stack.imgur.com/Tk89S.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tk89S.jpg" alt="enter image description here"></a></p>
  
  <p>For each new record in the table <strong>Liquidaciones</strong> I need set the field value <strong>comisiones_id</strong> (for example: 2), outside the form</p>
  
  <p>This is a special case because <strong>comisiones_id</strong> field is related to the table <strong>Comisiones</strong></p>
  
  <p>When I try to set the value, in <strong>LiquidacionesController.php</strong> with:</p>
  
  <pre><code> $liquidacione = new Liquidaciones();
      $form = $this-&gt;createForm('BackendBundle\Form\LiquidacionesType', $liquidacione);
      $form-&gt;get('comisiones')-&gt;setData('2');
  
  
      $form-&gt;handleRequest($request);
  </code></pre>
  
  <p>I get the following error message:</p>
  
  <pre><code>Catchable Fatal Error: Argument 1 passed to BackendBundle\Entity\Liquidaciones::setComisiones() must be an instance of BackendBundle\Entity\Comisiones, string given, called in C:\proyectos\cursospierobis\vendor\symfony\symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php on line 556 and defined 
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>Here my files:</p>
  
  <p><strong>LiquidacionesController.php</strong></p>
  
  <pre><code>   public function newAction(Request $request)
  {
      $liquidacione = new Liquidaciones();
      $form = $this-&gt;createForm('BackendBundle\Form\LiquidacionesType', $liquidacione);
          $form-&gt;get('comisiones')-&gt;setData('2');
      $form-&gt;handleRequest($request);
  
  
  
      if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($liquidacione);
          $em-&gt;flush();
  
          return $this-&gt;redirectToRoute('liquida_show', array('id' =&gt; $liquidacione-&gt;getId()));
      }
  
      return $this-&gt;render('liquidaciones/new.html.twig', array(
          'liquidacione' =&gt; $liquidacione,
          'form' =&gt; $form-&gt;createView(),
      ));
  }
  </code></pre>
  
  <p><strong>LiquidacionesType.php</strong></p>
  
  <pre><code>class LiquidacionesType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('nroLiqui')
              -&gt;add('fechaPago', null, array('widget' =&gt; 'single_text'))
              -&gt;add('monto')
              -&gt;add('observaciones')
              -&gt;add('comisiones', 'hidden')
                  ;
  
      }
  
  /**
   * @param OptionsResolver $resolver
   */
  public function configureOptions(OptionsResolver $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'BackendBundle\Entity\Liquidaciones'
      ));
  }
  }
  </code></pre>
  
  <p>And Entity <strong>Liquidaciones.php</strong>:</p>
  
  <pre><code>namespace BackendBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Liquidaciones
   *
   * @ORM\Table(name="liquidaciones", indexes={@ORM\Index(name="fk_Liquidaciones_Comisiones1_idx", columns={"comisiones_id"})})
   *  @ORM\Entity(repositoryClass="BackendBundle\Entity\LiquidacionesRepository")
   */
  
  ********************
  
    public function setComisiones(\BackendBundle\Entity\Comisiones $comisiones)
  {
      $this-&gt;comisiones = $comisiones;
  
      return $this;
  }
  
  /**
   * Get comisiones
   *
   * @return \BackendBundle\Entity\Comisiones 
   */
  public function getComisiones()
  {
      return $this-&gt;comisiones;
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Using the solution proposed by <strong>Iaroslav Gashuk</strong>, the code would look like:</p>
  
  <p><strong>LiquidacionesController.php</strong></p>
  
  <pre><code>public function newAction(Request $request )
  {
      $liquidacione = new Liquidaciones();
  
  
      $form = $this-&gt;createForm('BackendBundle\Form\LiquidacionesType', $liquidacione);
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {   
  
          $comisiones = $this-&gt;getDoctrine()-&gt;getManager()-&gt;getRepository('BackendBundle:Comisiones')-&gt;findOneById(4);
          $liquidacione-&gt;setComisiones($comisiones);
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $em-&gt;persist($liquidacione);
          $em-&gt;flush();
  
          return $this-&gt;redirectToRoute('liquidaciones_show', array('id' =&gt; $liquidacione-&gt;getId()));
      }
  
      return $this-&gt;render('liquidaciones/new.html.twig', array(
          'liquidacione' =&gt; $liquidacione,
          'form' =&gt; $form-&gt;createView(),
      ));
  }
  </code></pre>