私は最近 ZF2 の学習を開始しました。誰かがこれを手伝ってくれることを願っています。
私は、Rob Allen の Zend Framework 2 チュートリアル (@rob-allen に感謝します) に取り組んでいます。
また、@AlloVince と @Maciej How to set db adapter to Validator RecordExists in Zend Framework 2のソリューションを使用します (両方の作成者に感謝します)。 editActionでこのソリューションを使用しなかったため、混乱しました。
Fatal error: Call to a member function get() on a non-object
でわかり'adapter' => $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter')
ます。
1) Module.php に追加
public function getServiceConfig() { return array( 'invokables' => array( 'RegionModel' => 'FcLibraries\Model\Region', //<-- added it ), 'factories' => array( 'FcLibraries\Model\RegionTable' => function ($sm) { $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); $table = new RegionTable($dbAdapter); return $table; }, ), ); }
2) Region.php に追加
/** * @var */ protected $serviceLocator; /** * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator * @return Library */ public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { $this->serviceLocator = $serviceLocator; return $this; } /** * @return \Zend\ServiceManager\ServiceLocatorInterface */ public function getServiceLocator() { return $this->serviceLocator; }
と
$inputFilter->add($factory->createInput(array( 'name' => 'name', 'required' => true, 'filters' => $this->_filters, 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 1, 'max' => 30, ), ), array( 'name' => 'Db\NoRecordExists', 'options' => array( 'table' => $this->table, 'field' => 'name', //'exclude' => array( // 'field' => 'id', // 'value' => $this->id //), 'adapter' => $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'), ), ), ), )));
3) addAction の RegionController.php で
$model = $this->getServiceLocator()->get('RegionModel');
の代わりに$model = new Region();
。
これは addAction では問題なく機能しますが、editAction でどのように使用すればよいかわかりません。
じぶんの
public function editAction() { $id = (int)$this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('zfcadmin/region', array( 'action' => 'add' )); } $data = $this->getRegionTable()->get($id); $form = new RegionForm(); $form->bind($data); $form->get('submitBtn')->setAttribute('value', 'Save'); $request = $this->getRequest(); if ($request->isPost()) { $form->setInputFilter($data->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $this->getRegionTable()->save($form->getData()); return $this->redirect()->toRoute('zfcadmin/regions'); } } return array( 'id' => $id, 'form' => $form, ); }
私のRegionTableには次のコードがあります:
/** * @param \Zend\Db\Adapter\Adapter $adapter */ public function __construct(Adapter $adapter) { $this->adapter = $adapter; $this->resultSetPrototype = new ResultSet(); $this->resultSetPrototype->setArrayObjectPrototype(new Region()); $this->initialize(); } public function get($id) { $id = (int)$id; $rowSet = $this->select(array('id' => $id)); $row = $rowSet->current(); if (!$row) { throw new \Exception("Could not find row $id"); } return $row; }
私の質問に答えてくれるすべての人に感謝します。
よろしく、ルスラン。