title: symfony2 apply transformer to entity form field - empty array?
body: <p>I am trying to get a dataTransformer to work on an entity field in symfony 2.</p>
  
  <p>context:</p>
  
  <ul>
  <li><p>form displays sails that user can select (checkboxes)</p></li>
  <li><p>this is the first step in a multi-step sail ordering process (later steps display options available for each sail, colors, etc)</p></li>
  </ul>
  
  <p>This is my form type class:</p>
  
  <pre><code>use Symfony\Component\Form\AbstractType;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  use Symfony\Component\Form\FormBuilderInterface;
  use Co\QuoteBundle\Form\DataTransformer\SailCollectionToStringsTransformer;
  
  class PartsTypeStep1 extends AbstractType {
  
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {   
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'Co\QuoteBundle\Entity\Parts',));
  
      $resolver-&gt;setRequired(array('sailsAvailable', 'em'));
  }
  
  public function buildForm(FormBuilderInterface $formBuilder, array $options)
  {        
      $transformer = new SailCollectionToStringsTransformer($options['em']);
  
      $formBuilder-&gt;add(
          $formBuilder-&gt;create('mainsailparts', 'entity', array(
              'class' =&gt; 'CoQuoteBundle:Mainsail',
              'choices' =&gt; $options['sailsAvailable']['mains'],
              'multiple' =&gt; true,
              'expanded' =&gt; true,
              'label' =&gt; 'Mainsails',))
         -&gt;addModelTransformer($transformer)); //line 58
  }
  
  public function getName() {
      return 'partsStep1';
  }
  }
  </code></pre>
  
  <p>The above works with no errors, but does not display the transformed data. The view is:</p>
  
  <pre><code>__ Race main
  __ Cruising main
  </code></pre>
  
  <p>(<code>__</code> stands for checkbox)</p>
  
  <p>However, the view I want is:</p>
  
  <pre><code>__ Race main ($1400)
  __ Cruising main ($800)
  </code></pre>
  
  <p>The transformer I have is:</p>
  
  <pre><code>use Symfony\Component\Form\DataTransformerInterface;
  use Symfony\Component\Form\Exception\TransformationFailedException;
  use Doctrine\Common\Persistence\ObjectManager;
  use Co\QuoteBundle\Entity\Sail;
  use Doctrine\Common\Collections\ArrayCollection;
  
  class SailCollectionToStringsTransformer implements DataTransformerInterface
  {
  /**
   * @var ObjectManager
   */
  private $om;
  
  /**
   * @param ObjectManager $om
   */
  public function __construct(ObjectManager $om)
  {
      $this-&gt;om = $om;
  }
  
  /**
   * Transforms a collection of sails to a collection of strings.
   * @param  ISail|null $sail
   * @return string
   */
  public function transform($sailCollection)
  {
      if (null === $sailCollection) {
          return null;
      }
  
      $labels = new ArrayCollection();
  
      foreach($sailCollection as $sail){
          $labels[] = $sail-&gt;getName().' ($'.$sail-&gt;getBuildPrice().')';
      }
  
      return $labels;
  }
  
  //reverse transformer... not the issue (yet) because the forward transformer doesn't work
  
  }
  </code></pre>
  
  <p>When running this through the netbeans debugger, an empty array is passed to the transformer. However, if I change line 58 to <code>-&gt;addViewTransformer($transformer));</code> and debug, it correctly passes two booleans with the sail id's as the array keys to the transformer. Unfortunately, I can't use the <code>ViewTransformer</code> because that no longer contains the original strings to change.</p>
  
  <p>Why does the ArrayCollection that should contain the main sails get passed to the transformer as an empty ArrayCollection? The function returns an empty <code>$labels</code> collection.</p>
  
  <p>I'm not sure what I am doing wrong... Help is much appreciated!!!!
  Thanks.</p>