/** * Handle customer update */ public function updateAction() { $id = $this->params()->fromRoute('id'); $customerForm = $this->getCustomerForm(); $request = $this->getRequest(); if ($request->isPost()) { $customerEntity = $this->getCustomerService()->save($request->getPost()->toArray(), $id); if ($customerEntity) { return $this->redirect()->toRoute('customer/action', array('action' => 'update', 'id' => $customerEntity->getId())); } $customerForm->setMessages($this->getCustomerService()->getCustomerFilter()->getMessages()); $customerForm->setData($this->getCustomerService()->getCustomerFilter()->getValues()); } else { $customerEntity = $this->getCustomerService()->fetchSingleById($id); $customerHydrator = new CustomerHydrator(); $customerForm->setData($customerHydrator->extract($customerEntity)); } return new ViewModel(array('customerForm' => $customerForm)); }
/** * Update existing customer * * @param CustomerEntity $customerEntity * @return integer */ public function updateCustomer(CustomerEntity $customerEntity) { $hydrator = new CustomerHydrator(); $updateData = $hydrator->extract($customerEntity); return $this->update($updateData, array('id' => $customerEntity->getId())); }
public function testUpdateExistingCustomer() { $customerTable = new CustomerTable($this->adapter); $customerEntity = $customerTable->fetchSingleById(42); $customerEntity->setFirstname('Monika'); $customerEntity->setLastname('Musterfrau'); $customerTable->updateCustomer($customerEntity); $queryTable = $this->getConnection()->createQueryTable('loadCustomersOrderedByLastname', 'SELECT * FROM customers WHERE id = "42";'); $expectedRow = $queryTable->getRow(0); $hydrator = new CustomerHydrator(); $customerRow = $hydrator->extract($customerEntity); $this->assertEquals($expectedRow, $customerRow); }