Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of using the form filter from the entity which you gathered from the table you should instantiate a new entity via the service manager to use the database adapter. </p> <p>You have a few options:</p> <ol> <li>Move the input filter to its own class and instantiate via the service manager so your database adapter is injected.</li> <li>Change the prototype object in the table gateway factory to be instantiated via the service manager factory.</li> <li>Instantiate a separate entity via the service manager and get the input filter from there.</li> </ol> <p>I personally would go for option 1 as it separates the code better.</p> <p>Some examples:</p> <p><strong>Option 1 (my choice):</strong> </p> <p>This involves moving the filter to its own file and class, creating a factory for it whilst injecting the database adapter. We will then, in the controller, get the filter via the service manager and apply the filter to the form.</p> <p>So first move your filter to a file in ModName\src\ModName\Form\RegionFilter.php, obviosly replacing ModName with your module name.</p> <p>and change the code to like so:</p> <pre class="lang-php prettyprint-override"><code>&lt;?php namespace Region\Form; use Zend\InputFilter\Factory as InputFactory; use Zend\InputFilter\InputFilter; use Zend\InputFilter\InputFilterAwareInterface; use Zend\InputFilter\InputFilterInterface; use Zend\Db\Adapter\Adapter; class RegionFilter implements InputFilterAwareInterface { /** * @var inputFilter */ protected $inputFilter; /** * @var Database Adapter */ protected $dbAdapter; /** * @param \Zend\InputFilter\InputFilterInterface $inputFilter * @throws \Exception */ public function setInputFilter(InputFilterInterface $inputFilter) { throw new \Exception("Not used"); } /** * @param \Zend\Db\Adapter $dbAdapter */ public function __construct(Adapter $dbAdapter) { $this-&gt;dbAdapter = $dbAdapter; } /** * * @return Zend\Db\Adapter */ public function getDbAdapter() { return $this-&gt;dbAdapter; } /** * @return \Zend\InputFilter\InputFilter * * Get the input filter (build it first) */ public function getInputFilter() { if (!$this-&gt;inputFilter) { $inputFilter = new InputFilter(); $factory = new InputFactory(); $inputFilter-&gt;add($factory-&gt;createInput(array( 'name' =&gt; 'name', 'required' =&gt; true, 'filters' =&gt; $this-&gt;_filters, 'validators' =&gt; array( array( 'name' =&gt; 'StringLength', 'options' =&gt; array( 'encoding' =&gt; 'UTF-8', 'min' =&gt; 1, 'max' =&gt; 30, ), ), array( 'name' =&gt; 'Db\NoRecordExists', 'options' =&gt; array( 'table' =&gt; $this-&gt;table, 'field' =&gt; 'name', //'exclude' =&gt; array( // 'field' =&gt; 'id', // 'value' =&gt; $this-&gt;id //), 'adapter' =&gt; $this-&gt;getDbAdapter(), ), ), ), ))); $this-&gt;inputFilter = $inputFilter; } return $this-&gt;inputFilter; } } ?&gt; </code></pre> <p>You would then create a factory like so in Module.php:</p> <pre class="lang-php prettyprint-override"><code>public function getServiceConfig() { return array( 'factories' =&gt; array( 'ModName\Form\RegionFilter' =&gt; function($sm) { $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter'); return new RegionFilter($dbAdapter); }, ), ); } </code></pre> <p>And finally in your controller, just do the following:</p> <pre class="lang-php prettyprint-override"><code>if ($request-&gt;isPost()) { $filter = $this-&gt;getServiceLocator()-&gt;get('ModName\Form\RegionFilter'); $form-&gt;setInputFilter($filter-&gt;getInputFilter()); $form-&gt;setData($request-&gt;getPost()); if ($form-&gt;isValid()) { $this-&gt;getRegionTable()-&gt;save($form-&gt;getData()); return $this-&gt;redirect()-&gt;toRoute('zfcadmin/regions'); } } </code></pre> <p><strong>Option 2:</strong></p> <p>This involves constructing your table with an instance of <code>Region</code> injected. Then you can set the prototype to this.</p> <p>So in your table construct:</p> <pre class="lang-php prettyprint-override"><code>public function __construct(Adapter $adapter, Region $region) { $this-&gt;adapter = $adapter; $this-&gt;resultSetPrototype = new ResultSet(); $this-&gt;resultSetPrototype-&gt;setArrayObjectPrototype($region); $this-&gt;initialize(); } </code></pre> <p>And then your factory:</p> <pre class="lang-php prettyprint-override"><code>public function getServiceConfig() { return array( 'invokables' =&gt; array( 'RegionModel' =&gt; 'FcLibraries\Model\Region', ), 'factories' =&gt; array( 'FcLibraries\Model\RegionTable' =&gt; function ($sm) { $region = $sm-&gt;get('RegionModel'); $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter'); $table = new RegionTable($dbAdapter,$region); return $table; }, ), ); } </code></pre> <p>You should be able to leave the rest of the code as is. Eg the controller. Now I have not tested this method so I'm not 100% it will work, but I think it should. The other two methods I have used previously myself.</p> <p><strong>Option 3 (the simplest):</strong> </p> <p>This involves getting a separate region model via the service manager and using that to apply the input filter to the form.</p> <pre class="lang-php prettyprint-override"><code>public function editAction() { $id = (int)$this-&gt;params()-&gt;fromRoute('id', 0); if (!$id) { return $this-&gt;redirect()-&gt;toRoute('zfcadmin/region', array( 'action' =&gt; 'add' )); } $data = $this-&gt;getRegionTable()-&gt;get($id); $form = new RegionForm(); $form-&gt;bind($data); $form-&gt;get('submitBtn')-&gt;setAttribute('value', 'Save'); $request = $this-&gt;getRequest(); if ($request-&gt;isPost()) { $region = $this-&gt;getServiceLocator()-&gt;get('RegionModel'); $form-&gt;setInputFilter($region-&gt;getInputFilter()); $form-&gt;setData($request-&gt;getPost()); if ($form-&gt;isValid()) { $this-&gt;getRegionTable()-&gt;save($form-&gt;getData()); return $this-&gt;redirect()-&gt;toRoute('zfcadmin/regions'); } } return array( 'id' =&gt; $id, 'form' =&gt; $form, ); } </code></pre> <p>I have not tested the code but you should get the gist. Any questions just ask.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload