Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set db adapter to Validator NoRecordExists and use it in controller?
    primarykey
    data
    text
    <p>I recently started learning ZF2 and hope someone can help me with this.</p> <p>I am working my way through Rob Allen's Zend Framework 2 Tutorial (many thanks to @rob-allen).</p> <p>Also I use a solution of @AlloVince and @Maciej <a href="/questions/13818525" title="example">How to set db adapter to Validator RecordExists in Zend Framework 2</a> (many thanks to both authors for it) and I confused because didn't to use this solution in <strong>editAction</strong>.</p> <p>I see <code>Fatal error: Call to a member function get() on a non-object</code> in <code>'adapter' =&gt; $this-&gt;getServiceLocator()-&gt;get('Zend\Db\Adapter\Adapter')</code>.</p> <p>1) In the Module.php added</p> <blockquote> <pre><code>public function getServiceConfig() { return array( 'invokables' =&gt; array( 'RegionModel' =&gt; 'FcLibraries\Model\Region', //&lt;-- added it ), 'factories' =&gt; array( 'FcLibraries\Model\RegionTable' =&gt; function ($sm) { $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter'); $table = new RegionTable($dbAdapter); return $table; }, ), ); } </code></pre> </blockquote> <p>2) In the Region.php added</p> <blockquote> <pre><code>/** * @var */ protected $serviceLocator; /** * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator * @return Library */ public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { $this-&gt;serviceLocator = $serviceLocator; return $this; } /** * @return \Zend\ServiceManager\ServiceLocatorInterface */ public function getServiceLocator() { return $this-&gt;serviceLocator; } </code></pre> </blockquote> <p>and</p> <blockquote> <pre><code> $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;getServiceLocator()-&gt;get('Zend\Db\Adapter\Adapter'), ), ), ), ))); </code></pre> </blockquote> <p>3) In the RegionController.php in addAction using</p> <blockquote> <p>$model = $this->getServiceLocator()->get('RegionModel');</p> </blockquote> <p>instead of <code>$model = new Region();</code>.</p> <p>This works fine for addAction, But I can not understand how I should use it in editAction.</p> <p>My</p> <blockquote> <pre><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()) { $form-&gt;setInputFilter($data-&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> </blockquote> <p>My RegionTable has the following code:</p> <blockquote> <pre><code>/** * @param \Zend\Db\Adapter\Adapter $adapter */ public function __construct(Adapter $adapter) { $this-&gt;adapter = $adapter; $this-&gt;resultSetPrototype = new ResultSet(); $this-&gt;resultSetPrototype-&gt;setArrayObjectPrototype(new Region()); $this-&gt;initialize(); } public function get($id) { $id = (int)$id; $rowSet = $this-&gt;select(array('id' =&gt; $id)); $row = $rowSet-&gt;current(); if (!$row) { throw new \Exception("Could not find row $id"); } return $row; } </code></pre> </blockquote> <p>Many thanks to all who will answer my question.</p> <p>Best regards, Ruslan.</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. 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