title: Hydrating ZF2 Form with Doctrine Entities
body: <p>I have problems Hydrating my form. When i pull my entity from the database my <code>OrderLinePerson</code> field is filled correctly with the connected entity. When trying to Hydrate the form it is not. </p>
  
  <p>Below some example code:</p>
  
  <pre><code>&lt;?php
  
  namespace SenetOrder\Form;
  
  use ZF2Core\Form\AbstractForm;
  use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
  use SenetOrder\Entity\OrderLine;
  use SenetOrder\Form\OrderLinePersonFieldset;
  
  class OrderLineForm extends AbstractForm
  {
  
      protected $organisation;
  
      public function __construct($name, $organisation)
      {
          $this-&gt;organisation = $organisation;
  
          parent::__construct($name);
  
          $this-&gt;setAttribute('method', 'post');
          $this-&gt;setAttribute('class', 'form-horizontal');
      }
  
      public function init()
      {
          $this-&gt;setHydrator(new DoctrineHydrator($this-&gt;getEntityManager()));
          $this-&gt;setObject(new OrderLine());
  
          $this-&gt;add([
              'name'       =&gt; 'orderLineType',
              'type'       =&gt; 'DoctrineModule\Form\Element\ObjectSelect',
              'options'    =&gt; [
                  'object_manager'             =&gt; $this-&gt;getEntityManager(),
                  'target_class'               =&gt; 'SenetOrder\Entity\OrderLineType',
                  'property'                   =&gt; 'name',
                  'disable_inarray_validator'  =&gt; true,
                  'empty_option'               =&gt; '',
                  'label'                      =&gt; _('Type'),
                  'label_attributes'           =&gt; [
                      'class' =&gt; 'col-lg-3 control-label'
                  ],
              ],
              'attributes' =&gt; [
                  'class' =&gt; 'form-control',
              ],
          ]);
  
          $this-&gt;add(array(
              'name'       =&gt; 'start',
              'type'       =&gt; 'date',
              'options'    =&gt; array(
                  'label'              =&gt; _('Start date:'),
                  'label_attributes'   =&gt; array(
                      'class' =&gt; 'col-lg-3 control-label'
                  ),
                  'format'             =&gt; 'd-m-Y',
              ),
              'attributes' =&gt; array(
                  'type'           =&gt; 'text',
                  'class'          =&gt; 'form-control datepicker',
                  'placeholder'    =&gt; 'dd-mm-yyyy',
                  'value'          =&gt; date('d-m-Y'),
              ),
          ));
  
          $this-&gt;add(array(
              'name'       =&gt; 'end',
              'type'       =&gt; 'date',
              'options'    =&gt; array(
                  'label'              =&gt; _('End date:'),
                  'label_attributes'   =&gt; array(
                      'class' =&gt; 'col-lg-3 control-label'
                  ),
                  'format'             =&gt; 'd-m-Y',
              ),
              'attributes' =&gt; array(
                  'type'           =&gt; 'text',
                  'class'          =&gt; 'form-control datepicker',
                  'placeholder'    =&gt; 'dd-mm-yyyy',
              ),
          ));
  
          $this-&gt;add([
              'type'       =&gt; 'SenetOrder\Form\OrderLinePersonFieldset',
          ]);
  
          $this-&gt;add([
              'name'       =&gt; 'submit',
              'type'       =&gt; 'submit',
              'options'    =&gt; [
                  'label' =&gt; _('Save'),
              ],
              'attributes' =&gt; [
                  'class' =&gt; 'btn btn-large btn-primary',
              ],
          ]);
      }
  
  }
  </code></pre>
  
  <p>The connected Fieldset
      
  
  <pre><code>namespace SenetOrder\Form;
  
  use Zend\Form\Fieldset;
  use SenetOrder\Entity\OrderLinePerson;
  use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
  
  class OrderLinePersonFieldset extends Fieldset
  {
      protected $serviceManager;
  
      public function __construct($name, $entityManager, $serviceManager, $organisation)
      {
          $this-&gt;organisation = $organisation;
          $this-&gt;serviceManager = $serviceManager;
  
          parent::__construct($name);
  
          $this-&gt;setHydrator(new DoctrineHydrator($entityManager));
          $this-&gt;setObject(new OrderLinePerson());
  
          $this-&gt;add([
              'name'       =&gt; 'person',
              'type'       =&gt; 'DoctrineModule\Form\Element\ObjectSelect',
              'options'    =&gt; [
                  'disable_inarray_validator'  =&gt; true,
                  'object_manager'             =&gt; $entityManager,
                  'target_class'               =&gt; 'SenetPerson\Entity\Person',
                  'label'                      =&gt; _('Person'),
                  'label_attributes'           =&gt; [
                      'class' =&gt; 'col-lg-3 control-label'
                  ],
                  'value_options'              =&gt; $this-&gt;getOrganisationPersonOptions(),
              ],
              'attributes' =&gt; [
                  'class' =&gt; 'form-control',
              ],
          ]);
      }
  
      protected function getOrganisationPersonOptions()
      {
          $data = [''];
          if(!is_null($this-&gt;organisation))
          {
              $service = $this-&gt;serviceManager-&gt;get('senetperson_service_person');
              $collection = $service-&gt;getBySearch(null, null, 'employee')-&gt;getResult();
              $data = [];
              foreach($collection as $person)
              {
                  $data[$person-&gt;getPersonId()] = $person-&gt;parseFullname();
              }
          }
          return count($data) === 0 ? [''] : $data;
      }
  
  }
  </code></pre>
  
  <p>The action how the handle the form</p>
  
  <pre><code>&lt;?php
  public function addAction()
  {
      $orderLine = new OrderLine();
      $orderLine-&gt;setOrder($this-&gt;order);
  
      $request = $this-&gt;getRequest();
      $form = $this-&gt;getServiceLocator()-&gt;get('FormElementManager')-&gt;get('SenetOrder\Form\OrderLineForm');
      $form-&gt;bind($orderLine);
  
      // Handle request
      if($request-&gt;isPost())
      {
          $form-&gt;setData($request-&gt;getPost());
          if($form-&gt;isValid())
          {
              $orderLine-&gt;setCreated(new \DateTime());
  
              var_dump($request-&gt;getPost());
              var_dump($orderLine);die;
              die('hi there ok form');
  
              $this-&gt;getEntityManager()-&gt;persist($orderLine);
              $this-&gt;getEntityManager()-&gt;flush();
  
              $this-&gt;resultMessenger()-&gt;addSuccessMessage($this-&gt;getTranslator()-&gt;translate('The order has been successfully added'));
              return $this-&gt;redirect()-&gt;toRoute('order/view', array('orderId'=&gt;$order-&gt;getOrderId()));
          }
          $this-&gt;resultMessenger()-&gt;addErrorMessage('There are errors in your form', $form-&gt;getMessages());
      }
  
      // Set breadcrumb label
      $nav = $this-&gt;getServiceLocator()-&gt;get('breadcrumb_navigation');
      $page = $nav-&gt;findByLabel(':name');
      $page-&gt;setLabel($this-&gt;order-&gt;getName());
  
      return new ViewModel(array(
          'form' =&gt; $form,
      ));
  }
  </code></pre>
  
  <p>Above is the code that is executed. Below a dump of the entity after the <code>$form-&gt;setData()</code> is executed. Also the dump of the request object</p>
  
  <pre><code>&lt;?php
  ## REQUEST
  object(Zend\Stdlib\Parameters)[193]
    public 'orderLineType' =&gt; string '1' (length=1)
    public 'start' =&gt; string '01-09-2014' (length=10)
    public 'end' =&gt; string '29-09-2014' (length=10)
    public 'orderLinePerson' =&gt; 
      array (size=1)
        'person' =&gt; string '286512' (length=6)
    public 'submit' =&gt; string '' (length=0)
  
  ## ENTITY DUMP
  object(SenetOrder\Entity\OrderLine)[861]
    protected 'orderLineId' =&gt; null
    protected 'order' =&gt; 
      object(SenetOrder\Entity\Order)[864]
        protected 'orderId' =&gt; string '1' (length=1)
        protected 'name' =&gt; string 'HÃ© leuk, orders in ZF2!' (length=24)
        protected 'description' =&gt; string 'HÃ© floepert, stiekem verder programmeren he!' (length=45)
        protected 'organisation' =&gt; 
          object(DoctrineORMModule\Proxy\__CG__\SenetOrganisation\Entity\Organisation)[901]
            public '__initializer__' =&gt; 
              object(Closure)[867]
                ...
            public '__cloner__' =&gt; 
              object(Closure)[868]
                ...
            public '__isInitialized__' =&gt; boolean false
            protected 'organisationId' =&gt; string '6300' (length=4)
            protected 'ownerPerson' =&gt; null
            protected 'otys' =&gt; null
            protected 'name' =&gt; null
            protected 'paymentInterval' =&gt; null
            protected 'vatNumber' =&gt; null
            protected 'debtor' =&gt; null
            protected 'invoiceByEmail' =&gt; null
            protected 'deleted' =&gt; null
            protected 'created' =&gt; null
            protected 'addressCollection' =&gt; null
            protected 'orderCollection' =&gt; null
            protected 'invoiceCollection' =&gt; null
        protected 'address' =&gt; 
          object(DoctrineORMModule\Proxy\__CG__\Application\Entity\Address)[904]
            public '__initializer__' =&gt; 
              object(Closure)[897]
                ...
            public '__cloner__' =&gt; 
              object(Closure)[896]
                ...
            public '__isInitialized__' =&gt; boolean false
            protected 'addressId' =&gt; string '56' (length=2)
            protected 'organisation' =&gt; null
            protected 'addressType' =&gt; null
            protected 'otys' =&gt; null
            protected 'address' =&gt; null
            protected 'postalcode' =&gt; null
            protected 'city' =&gt; null
            protected 'email' =&gt; null
            protected 'department' =&gt; null
            protected 'country' =&gt; null
            protected 'created' =&gt; null
            protected 'deleted' =&gt; null
        protected 'person' =&gt; 
          object(DoctrineORMModule\Proxy\__CG__\SenetPerson\Entity\Person)[933]
            public '__initializer__' =&gt; 
              object(Closure)[900]
                ...
            public '__cloner__' =&gt; 
              object(Closure)[881]
                ...
            public '__isInitialized__' =&gt; boolean false
            protected 'personId' =&gt; string '286278' (length=6)
            protected 'otys' =&gt; null
            protected 'initials' =&gt; null
            protected 'firstname' =&gt; null
            protected 'middlename' =&gt; null
            protected 'lastname' =&gt; null
            protected 'position' =&gt; null
            protected 'birthday' =&gt; null
            protected 'gender' =&gt; null
            protected 'file' =&gt; null
            protected 'created' =&gt; null
            protected 'deleted' =&gt; null
            protected 'personRoleCollection' =&gt; null
            protected 'personOrganisationCollection' =&gt; null
            protected 'personLogin' =&gt; null
            protected 'ownedOrderCollection' =&gt; null
            protected 'personOrderCollection' =&gt; null
            protected 'personHourCollection' =&gt; null
            protected 'personPhoneCollection' =&gt; null
            protected 'personEmailCollection' =&gt; null
            protected 'personContractCollection' =&gt; null
        protected 'created' =&gt; 
          object(DateTime)[862]
            public 'date' =&gt; string '2014-09-08 11:55:11.000000' (length=26)
            public 'timezone_type' =&gt; int 3
            public 'timezone' =&gt; string 'Europe/Amsterdam' (length=16)
        protected 'orderLineCollection' =&gt; 
          object(Doctrine\ORM\PersistentCollection)[921]
            private 'snapshot' =&gt; 
              array (size=0)
                ...
            private 'owner' =&gt; 
              &amp;object(SenetOrder\Entity\Order)[864]
            private 'association' =&gt; 
              array (size=16)
                ...
            private 'em' =&gt; 
              object(Doctrine\ORM\EntityManager)[351]
                ...
            private 'backRefFieldName' =&gt; string 'order' (length=5)
            private 'typeClass' =&gt; 
              object(Doctrine\ORM\Mapping\ClassMetadata)[932]
                ...
            private 'isDirty' =&gt; boolean false
            private 'initialized' =&gt; boolean false
            private 'coll' =&gt; 
              object(Doctrine\Common\Collections\ArrayCollection)[920]
                ...
    protected 'orderLineType' =&gt; 
      object(SenetOrder\Entity\OrderLineType)[1353]
        protected 'orderLineTypeId' =&gt; string '1' (length=1)
        protected 'name' =&gt; string 'Secondment' (length=10)
    protected 'start' =&gt; 
      object(DateTime)[1350]
        public 'date' =&gt; string '2014-09-01 00:00:00.000000' (length=26)
        public 'timezone_type' =&gt; int 3
        public 'timezone' =&gt; string 'Europe/Amsterdam' (length=16)
    protected 'end' =&gt; 
      object(DateTime)[1348]
        public 'date' =&gt; string '2014-09-29 00:00:00.000000' (length=26)
        public 'timezone_type' =&gt; int 3
        public 'timezone' =&gt; string 'Europe/Amsterdam' (length=16)
    protected 'created' =&gt; 
      object(DateTime)[1354]
        public 'date' =&gt; string '2014-09-12 12:18:38.000000' (length=26)
        public 'timezone_type' =&gt; int 3
        public 'timezone' =&gt; string 'Europe/Amsterdam' (length=16)
    protected 'deleted' =&gt; null
    protected 'orderLinePerson' =&gt; null
  </code></pre>
  
  <p>What i want to achieve is that the <code>orderLinePerson</code> field is filled with its object <code>OrderLinePerson</code></p>