public function updateAction() { $id = $this->params()->fromRoute('id'); if (!$id) { return $this->redirect()->toRoute('csn_user/default', array('controller' => 'user-row-gateway-feature-bind', 'action' => 'index')); } $form = new UserForm(); $form->setHydrator(new ObjectProperty()); $user = $this->getUsersTable()->select(array('usr_id' => $id))->current(); $form->bind($user); $request = $this->getRequest(); if ($request->isPost()) { $form->setInputFilter(new UserFilter()); $form->setData($request->getPost()); $form->remove('submit'); if ($form->isValid()) { $user->save(); return $this->redirect()->toRoute('csn_user/default', array('controller' => 'user-row-gateway-feature-bind', 'action' => 'index')); } $form->add(array('name' => 'submit', 'attributes' => array('type' => 'submit', 'value' => 'Go', 'id' => 'submitbutton'))); } else { // ToDo find why the form doesn't get populated from the object directly and remove this line $form->setData($this->getUsersTable()->select(array('usr_id' => $id))->current()->toArray()); } return new ViewModel(array('form' => $form, 'id' => $id)); }
public function updateAction() { $id = $this->params()->fromRoute('id'); if (!$id) { return $this->redirect()->toRoute('csn_user/default', array('controller' => 'user-hydrating-result-set-bind', 'action' => 'index')); } $form = new UserForm(); $form->setHydrator(new ReflectionHydrator()); $user = $this->getUsersTable()->select(array('usr_id' => $id))->current(); $form->bind($user); $request = $this->getRequest(); if ($request->isPost()) { $form->setInputFilter(new UserFilter()); $form->setData($request->getPost()); if ($form->isValid()) { // ToDo raplace the code with something that uses user object $data = $form->getData(); $hydrator = new ReflectionHydrator(); $data = $hydrator->extract($data); // turn the object to array unset($data['submit']); if (empty($data['usr_registration_date'])) { $data['usr_registration_date'] = '2013-07-19 12:00:00'; } $this->getUsersTable()->update($data, array('usr_id' => $id)); return $this->redirect()->toRoute('csn_user/default', array('controller' => 'user-hydrating-result-set-bind', 'action' => 'index')); } } return new ViewModel(array('form' => $form, 'id' => $id)); }
public function updateAction() { $id = $this->params()->fromRoute('id'); if (!$id) { return $this->redirect()->toRoute('csn_user/default', array('controller' => 'user-result-set-bind', 'action' => 'index')); } $form = new UserForm(); // $user = $this->getUsersTable()->select(array('usr_id' => $id))->current(); $user = $this->getUsersTable()->getUser($id); //- Error message: Zend\Stdlib\Hydrator\ArraySerializable::extract expects the provided object to implement getArrayCopy() $form->bind($user); $request = $this->getRequest(); if ($request->isPost()) { $form->setInputFilter(new UserFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $this->getUsersTable()->update($user); // 1) The question remains open: How to save the user object to the DB. We Need a data mapper. // We need an object that can extract and hidrate our object // DataMapper will be better coze I can use it as a Repository for SQL statements also // or // 2) The object should extend or composit RowDataGateway and use the save and delete methods. // The object should know how to save iyself // ToDo replce with code that uses the users object datamapper or RowDatagateway // takes care of saving the data //- $data = $form->getData(); //- unset($data['submit']); //- if (empty($data['usr_registration_date'])) $data['usr_registration_date'] = '2013-07-19 12:00:00'; //- $this->getUsersTable()->update($data, array('usr_id' => $id)); return $this->redirect()->toRoute('csn_user/default', array('controller' => 'user-result-set-bind', 'action' => 'index')); } } // we don't need to populate the form anymore it is done with bind return new ViewModel(array('form' => $form, 'id' => $id)); }