title: Symfony hiddenType using data_class for entity instead of transformer
body: <p>So i tried to store the id of one of my entities in the hiddenType and i got:</p>
  
  <p><em>The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class AppBundle\Entity\Users. You can avoid this error by setting the "data_class" option to "AppBundle\Entity\Users" or by adding a view transformer that transforms an instance of class AppBundle\Entity\Users to scalar, array or an instance of \ArrayAccess.</em></p>
  
  <p><strong>data_class</strong>: "This option is used to set the appropriate data mapper to be used by the form, so you can use it for any form field type which requires an object."</p>
  
  <p>see: <a href="http://symfony.com/doc/2.7/reference/forms/types/form.html#data-class" rel="nofollow">http://symfony.com/doc/2.7/reference/forms/types/form.html#data-class</a></p>
  
  <p>and so i fix my form:</p>
  
  <pre><code>$builder
      -&gt;add('user', 'hidden', array(
          'data_class' =&gt; 'AppBundle\Entity\User',
      ));
  </code></pre>
  
  <p>when i try this i get an exception stating that my entity cannot be translated to a string</p>
  
  <p>so i implement the __tostring magic method on my entity to return the entity’s id, then twig is able to put the entity id in the hidden field value</p>
  
  <p>then when i try to submit my form i get:</p>
  
  <p><em>Catchable Fatal Error: Argument 1 passed to AppBundle\Entity\Students::setUser() must be an instance of AppBundle\Entity\Users, string given, called in /vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 442 and defined</em> </p>
  
  <p>so its not able to pull the string value from the request back into an entity for use in my form.</p>
  
  <p>yes, i've seen the implementation where you build a entityHiddenType using a transformer. </p>
  
  <p>however i'm asking is this possible using the data_class setting provided by symphony as i believe this is the intended method to solve this?</p>
  
  <p>I just want to know if it can be achieved using data_class instead of a transformer. as well as which method is best practice.</p>