title: How to Extend a Fieldset in ZF2 to work with Doctrine’s Class Table Inheritance mapping strategy
body: <p>I’m developing a project using Doctrine’s Class Table Inheritance mapping strategy which involves joining a parent table with one of a number of child tables depending on the value in a discriminator column in the parent table.  I’ve got a working prototype in which the unique conglomerate fieldsets each contain duplicate copies of all of the code for the common elements from the parent entity.  In order to ensure consistency and avoid excess code I want to change the fieldsets so that I have a single fieldset that’s related to the parent entity and all of the other fieldsets are simply extensions of the parent (a detailed explanation is included at <a href="https://stackoverflow.com/questions/23240924">How to Extend a Fieldset in ZF2</a>).  I run into problems when I separate the fieldsets and then try to get them to work with each other.</p>
  
  <p>The first answer to the question <a href="https://stackoverflow.com/questions/23240924">How to Extend a Fieldset in ZF2</a> provides a clear explaination of how a single fieldset can extend another in ZF2.  However, the answer uses <code>init()</code> to put together the fieldsets, and for the Doctrine strategy we need to use <code>__construct</code>.  My first pass in developing the fieldsets was to build in the some of the code prescribed in the <a href="https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md" rel="nofollow noreferrer">DoctrineModule/docs</a>, which brings us to this:</p>
  
  <pre><code>class FieldsetParent extends Zend\Form\Fieldset
  {
     public function __construct(ObjectManager $objectManager) {
  
          parent::__construct('parent-fieldset');
  
          $this-&gt;setHydrator(new DoctrineHydrator($objectManager, 'MyModule\Entity\Parent'))
               -&gt;setObject(new Parent());
  
          $this-&gt;add(array('name' =&gt; 'fieldA'));
          $this-&gt;add(array('name' =&gt; 'fieldB'));
          $this-&gt;add(array('name' =&gt; 'fieldC'));
     }
  }
  </code></pre>
  
  <p>and this:</p>
  
  <pre><code>class FieldsetFoo extends FieldsetParent
  {
     public function __construct(ObjectManager $objectManager) {
  
          parent::__construct('foo-fieldset');
  
          $this-&gt;setHydrator(new DoctrineHydrator($objectManager, 'MyModule\Entity\Foo'))
               -&gt;setObject(new Foo());
  
          $this-&gt;add(array('name' =&gt; 'fieldD'));
          $this-&gt;add(array('name' =&gt; 'fieldE'));
          $this-&gt;add(array('name' =&gt; 'fieldF'));
          $this-&gt;add(array('name' =&gt; 'fieldG'));
     }
  }
  </code></pre>
  
  <p>This doesn’t work because it attempts to add a fieldset from a string, giving the error message: </p>
  
  <pre><code>Catchable fatal error: Argument 1 passed to MyModuule\Form\ParentFieldset::__construct() 
  must implement interface Doctrine\Common\Persistence\ObjectManager, string given ...
  </code></pre>
  
  <p>The first answer to the question <a href="https://stackoverflow.com/questions/19081982">zend2 doctrine 2 form intgergration OneToOne</a> explains how adding a fieldset from a string can be avoided in the case of a OneToOne strategy.  However, I'm working with a different ORM strategy and I’m having difficulties working out how to solve the same problem.</p>
  
  <h1><strong>EDIT</strong></h1>
  
  <p>As requested, here is some more detailed information:</p>
  
  <pre><code>class FooController extends AbstractActionController
  {
      /**
       * @var Doctrine\ORM\EntityManager
       */
      protected $em;
  
      public function setEntityManager(EntityManager $em)
      {
          $this-&gt;em = $em;
          return $this;
      }
  
      public function getEntityManager()
      {
          if (null === $this-&gt;em) {
              $this-&gt;em = $this-&gt;getServiceLocator()-&gt;get('Doctrine\ORM\EntityManager');
          }
          return $this-&gt;em;
      }
  
      // ... //
  
      public function editAction()
      {
          $fooID = (int)$this-&gt;getEvent()-&gt;getRouteMatch()-&gt;getParam('fooID');
          if (!$fooID) {
              return $this-&gt;redirect()-&gt;toRoute('foo', array('action'=&gt;'add'));
          }
  
          $foo = $this-&gt;getEntityManager()-&gt;find('MyModule\Entity\Foo', $fooID);
  
          // Get your ObjectManager from the ServiceManager
          $objectManager = $this-&gt;getServiceLocator()-&gt;get('Doctrine\ORM\EntityManager');
  
          // Create the form and inject the ObjectManager
          $form = new EditFooForm($objectManager);
          $form-&gt;setBindOnValidate(false);
          $form-&gt;bind($foo);
          $form-&gt;get('submit')-&gt;setAttribute('label', 'Update');
  
          $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('foo');
              }
          }
  
          return array(
              'foo' =&gt; $foo,
              'form' =&gt; $form,
          );
      }
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class EditFooForm extends Form
  {
      public function __construct(ObjectManager $objectManager)
      {
          parent::__construct('edit_foo_form');
  
          $this-&gt;setHydrator(new DoctrineHydrator($objectManager, 'MyModule\Entity\Foo'));
  
          $fooFieldset = new FooFieldset($objectManager);
          $fooFieldset-&gt;setUseAsBaseFieldset(true);
          $this-&gt;add($fooFieldset);
  
          // submit elements
      }
  }
  </code></pre>
  
  <p>This fieldset works:</p>
  
  <pre><code>class FooFieldset extends Fieldset implements InputFilterProviderInterface
  {
      public function __construct(ObjectManager $objectManager)
      {
          parent::__construct('foo-fieldset');
  
          $this-&gt;setHydrator(new DoctrineHydrator($objectManager, 'MyModule\Entity\Foo'))
               -&gt;setObject(new Foo());
  
          $this-&gt;add(array('name' =&gt; 'fieldA'));
          $this-&gt;add(array('name' =&gt; 'fieldB'));
          $this-&gt;add(array('name' =&gt; 'fieldC'));
          $this-&gt;add(array('name' =&gt; 'fieldD'));
          $this-&gt;add(array('name' =&gt; 'fieldE'));
          $this-&gt;add(array('name' =&gt; 'fieldF'));
          $this-&gt;add(array('name' =&gt; 'fieldG'));
  
      }
  
      public function getInputFilterSpecification()
      {
          return array('fieldA' =&gt; array('required' =&gt; false),);
          return array('fieldB' =&gt; array('required' =&gt; false),);
          return array('fieldC' =&gt; array('required' =&gt; false),);
          return array('fieldD' =&gt; array('required' =&gt; false),);
          return array('fieldE' =&gt; array('required' =&gt; false),);
          return array('fieldF' =&gt; array('required' =&gt; false),);
          return array('fieldG' =&gt; array('required' =&gt; false),);
  
      }
  
  }
  </code></pre>
  
  <p>These fieldsets give an error message:</p>
  
  <pre><code>class FoobarFieldset extends Fieldset implements InputFilterProviderInterface
  {
      public function __construct(ObjectManager $objectManager)
      {
          parent::__construct('foobar-fieldset');
  
          $this-&gt;setHydrator(new DoctrineHydrator($objectManager, 'MyModule\Entity\Foobar'))
               -&gt;setObject(new Foobar());
  
          $this-&gt;add(array('name' =&gt; 'fieldA'));
          $this-&gt;add(array('name' =&gt; 'fieldB'));
          $this-&gt;add(array('name' =&gt; 'fieldC'));
  
      }
  
      public function getInputFilterSpecification()
      {
          return array('fieldA' =&gt; array('required' =&gt; false),);
          return array('fieldB' =&gt; array('required' =&gt; false),);
          return array('fieldC' =&gt; array('required' =&gt; false),);
  
      }
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>use MyModule\Form\FoobarFieldset;
  
  class FooFieldset extends FoobarFieldset implements InputFilterProviderInterface
  {
      public function __construct(ObjectManager $objectManager)
      {
          parent::__construct('foo-fieldset');
  
          $this-&gt;setHydrator(new DoctrineHydrator($objectManager, 'MyModule\Entity\Foo'))
               -&gt;setObject(new Foo());
  
          $this-&gt;add(array('name' =&gt; 'fieldD'));
          $this-&gt;add(array('name' =&gt; 'fieldE'));
          $this-&gt;add(array('name' =&gt; 'fieldF'));
          $this-&gt;add(array('name' =&gt; 'fieldG'));
  
      }
  
      public function getInputFilterSpecification()
      {
          return array('fieldD' =&gt; array('required' =&gt; false),);
          return array('fieldE' =&gt; array('required' =&gt; false),);
          return array('fieldF' =&gt; array('required' =&gt; false),);
          return array('fieldG' =&gt; array('required' =&gt; false),);
  
      }
  
  }
  </code></pre>
  
  <p>The resulting error message is:</p>
  
  <pre><code>Catchable fatal error: Argument 1 passed to MyModule\Form\FoobarFieldset::__construct() 
  must implement interface Doctrine\Common\Persistence\ObjectManager, string given, called
  in C:\xampp\htdocs\GetOut\module\MyModule\src\MyModule\Form\FooFieldset.php on line 17 
  and defined in C:\xampp\htdocs\GetOut\module\MyModule\src\MyModule\Form\FoobarFieldset.php
  on line 14
  </code></pre>
  
  <p>I have tried to avoid adding a fieldset from a string by making these changes:</p>
  
  <pre><code>use MyModule\Form\FoobarFieldset;
  
  class FooFieldset extends Fieldset implements InputFilterProviderInterface
  {
      public function __construct(ObjectManager $objectManager)
      {
          parent::__construct('foo-fieldset');
  
          $this-&gt;setHydrator(new DoctrineHydrator($objectManager, 'MyModule\Entity\Foo'))
               -&gt;setObject(new Foo());
  
          $this-&gt;add(array('name' =&gt; 'fieldD'));
          $this-&gt;add(array('name' =&gt; 'fieldE'));
          $this-&gt;add(array('name' =&gt; 'fieldF'));
          $this-&gt;add(array('name' =&gt; 'fieldG'));
  
          $fieldset = new FoobarFieldset($objectManager);
          $this-&gt;add($fieldset);
  
      }
  </code></pre>
  
  <p>but this gives a <code>Zend\Form\Exception\InvalidElementException</code> with the message, <code>No element by the name of [fieldA] found in form</code>, indicating that the filedset is not getting added.</p>
  
  <h1>AN ALTERNATIVE</h1>
  
  <p>When it’s all said and done, all I’m really trying to do is keep all of the common statements in one place and draw them into the various unique fieldsets that need to include them.  I can solve this without ZF2 by using <code>include</code> statements as shown below:</p>
  
  <pre><code>// FoobarFieldset_fields.php
  
  $this-&gt;add(array('name' =&gt; 'fieldA'));
  $this-&gt;add(array('name' =&gt; 'fieldB'));
  $this-&gt;add(array('name' =&gt; 'fieldC'));
  </code></pre>
  
  <p>and</p>
  
  <pre><code>// FoobarFieldset_filters.php
  
  return array('fieldA' =&gt; array('required' =&gt; false),);
  return array('fieldB' =&gt; array('required' =&gt; false),);
  return array('fieldC' =&gt; array('required' =&gt; false),);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class FooFieldset extends Fieldset implements InputFilterProviderInterface
  {
      public function __construct(ObjectManager $objectManager)
      {
          parent::__construct('foo-fieldset');
  
          $this-&gt;setHydrator(new DoctrineHydrator($objectManager, 'MyModule\Entity\Foo'))
               -&gt;setObject(new Foo());
  
          include 'FoobarFieldset_fields.php';
          $this-&gt;add(array('name' =&gt; 'fieldD'));
          $this-&gt;add(array('name' =&gt; 'fieldE'));
          $this-&gt;add(array('name' =&gt; 'fieldF'));
          $this-&gt;add(array('name' =&gt; 'fieldG'));
  
      }
  
      public function getInputFilterSpecification()
      {
          include 'FoobarFieldset_filters.php';
          return array('fieldD' =&gt; array('required' =&gt; false),);
          return array('fieldE' =&gt; array('required' =&gt; false),);
          return array('fieldF' =&gt; array('required' =&gt; false),);
          return array('fieldG' =&gt; array('required' =&gt; false),);
  
      }
  
  }
  </code></pre>