title: Zend Framework 2 - Hydrator strategy for Doctrine relationship not working
body: <p>As mentioned <a href="https://stackoverflow.com/q/14140447/1331671">here</a> I'm building a custom hydration strategy to handle my related objects in a select box in a form.</p>
  
  <p>My form looks like this:</p>
  
  <pre><code>$builder = new AnnotationBuilder($entityManager);
  $form    = $builder-&gt;createForm(new MyEntity());
  $form-&gt;add(new MyFieldSet());
  
  $hydrator = new ClassMethodsHydrator();
  $hydrator-&gt;addStrategy('my_attribute', new MyHydrationStrategy());
  $form-&gt;setHydrator($hydrator);
  
  $form-&gt;get('my_attribute')-&gt;setValueOptions(
        $entityManager-&gt;getRepository('SecEntity\Entity\SecEntity')-&gt;fetchAllAsArray()
  );
  </code></pre>
  
  <p><strong>When I add a new <code>MyEntity</code> via the <code>addAction</code> everything works great.</strong></p>
  
  <p>I wrote <code>fetchAllAsArray()</code> to populate my selectbox. It lives within my SecEntityRepository:</p>
  
  <pre><code>public function fetchAllAsArray() {
  
      $objects = $this-&gt;createQueryBuilder('s')
          -&gt;add('select', 's.id, s.name')
          -&gt;add('orderBy', 's.name ASC')
          -&gt;getQuery()
          -&gt;getResult();
  
      $list = array();
      foreach($objects as $obj) {
          $list[$obj['id']] = $obj['name'];
      }
  
      return $list;
  }
  </code></pre>
  
  <p>But in the edit-case the <code>extract()</code> function doesn't work. I'm not at the point where I see something of <code>hydrate()</code> so I'll leave it out for now.</p>
  
  <p>My hydrator strategy looks like this:</p>
  
  <pre><code>class MyHydrationStrategy extends DefaultStrategy
  {
      public function extract($value) {        
          print_r($value);
          $result = array();
          foreach ($value as $instance) {
              print_r($instance);
              $result[] = $instance-&gt;getId();
          }
          return $result;
      }
  
      public function hydrate($value) {
          ...
      }
  </code></pre>
  
  <p>The problem is as follows: </p>
  
  <blockquote>
    <p>Fatal error: Call to a member function getId() on a non-object</p>
  </blockquote>
  
  <p>The <code>print_r($value)</code> returns loads of stuff beginning with</p>
  
  <blockquote>
    <p>DoctrineORMModule\Proxy__CG__\SecEntity\Entity\SecEntity Object</p>
  </blockquote>
  
  <p>following with something about BasicEntityPersister and somewhere in the mess are my referenced entities.</p>
  
  <p>The <code>print_r($instance)</code> prints nothing. It's just empty. Therefore I guess is the error message legit... but why can't I iterate over these objects?</p>
  
  <p>Any ideas?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Regarding to @Sam:</p>
  
  <p>My attribute in the entity:</p>
  
  <pre><code>    /**
   * @ORM\ManyToOne(targetEntity="Path/To/Entity", inversedBy="whatever")
   * @ORM\JoinColumn(name="attribute_id", referencedColumnName="id")
   * @Form\Attributes({"type":"hidden"})
   *
   */
  protected $attribute;
  </code></pre>
  
  <p>My new selectbox:</p>
  
  <pre><code>$form-&gt;add(array(
          'name'       =&gt; 'attribute',
          'type'       =&gt; 'DoctrineModule\Form\Element\ObjectSelect',
          'attributes' =&gt; array(
              'required' =&gt; true
          ),
          'options'    =&gt; array(
              'label'           =&gt; 'MyLabel',
              'object_manager'  =&gt; $entityManager,
              'target_class'    =&gt; 'Path/To/Entity',
              'property'        =&gt; 'name'
          )
      ));
  </code></pre>
  
  <p>My final hope is that I'm doing something wrong within the controller. Neither my selectbox is preselected nor the value is saved...</p>
  
  <pre><code>...
  
  $obj= $this-&gt;getEntityManager()-&gt;find('Path/To/Entity', $id);
  
      $builder = new \MyEnity\MyFormBuilder();
      $form = $builder-&gt;newForm($this-&gt;getEntityManager());
  
      $form-&gt;setBindOnValidate(false);
      $form-&gt;bind($obj);
      $form-&gt;setData($obj-&gt;getArrayCopy());
  
      $request = $this-&gt;getRequest();
      if ($request-&gt;isPost()) {
          $form-&gt;setData($request-&gt;getPost());
  
          if ($form-&gt;isValid()) {
              $form-&gt;bindValues();
              $this-&gt;getEntityManager()-&gt;flush();
  
              return $this-&gt;redirect()-&gt;toRoute('entity');
          }
      }
  </code></pre>