public function editAction()
 {
     $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         return $this->redirect()->toRoute('mvitclient', array('action' => 'add'));
     }
     $client = $this->clientService->findClientById($id);
     $form = new ClientForm();
     $request = $this->getRequest();
     if ($this->request->isPost() && !isset($request->getPost('client')["emailAddresses"])) {
         $client->setEmailAddresses(array());
     }
     if ($this->request->isPost() && !isset($request->getPost('client')["phoneNumbers"])) {
         $client->setPhoneNumbers(array());
     }
     if ($this->request->isPost() && !isset($request->getPost('client')["postAddresses"])) {
         $client->setPostAddresses(array());
     }
     $form->bind($client);
     if ($request->isPost()) {
         $form->setInputFilter($client->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $this->clientService->saveClient($client);
             // Redirect to list of clients
             return $this->redirect()->toRoute('mvitclient');
         }
     }
     return array('id' => $id, 'form' => $form);
 }
 public function editAction()
 {
     $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         return $this->redirect()->toRoute('mvitclient', array('action' => 'add'));
     }
     $client = $this->getClientTable()->getClientById($id);
     foreach ($this->getEmailAddressTable()->getEmailAddressByOwner($client->getId()) as $emailaddress) {
         $client->addEmailAddress($emailaddress);
     }
     foreach ($this->getPhoneNumberTable()->getPhoneNumberByOwner($client->getId()) as $phonenumber) {
         $client->addPhoneNumber($phonenumber);
     }
     foreach ($this->getPostAddressTable()->getPostAddressByOwner($client->getId()) as $postaddress) {
         $client->addPostAddress($postaddress);
     }
     $form = new ClientForm();
     $request = $this->getRequest();
     if ($this->request->isPost() && !isset($request->getPost('client')["emailAddresses"])) {
         $client->setEmailAddresses(array());
     }
     if ($this->request->isPost() && !isset($request->getPost('client')["phoneNumbers"])) {
         $client->setPhoneNumbers(array());
     }
     if ($this->request->isPost() && !isset($request->getPost('client')["postAddresses"])) {
         $client->setPostAddresses(array());
     }
     $form->bind($client);
     if ($request->isPost()) {
         $form->setInputFilter($client->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $this->getClientTable()->saveClient($client);
             $arrId = array();
             foreach ($client->getPhoneNumbers() as $phoneNumber) {
                 $phoneNumber->setOwner($client->getId());
                 $this->getPhoneNumberTable()->savePhoneNumber($phoneNumber);
                 $arrId[] = $phoneNumber->getId();
             }
             $this->getPhoneNumberTable()->deleteRemovedPhoneNumbers($client->getId(), $arrId);
             $arrId = array();
             foreach ($client->getEmailAddresses() as $emailAddress) {
                 $emailAddress->setOwner($client->getId());
                 $this->getEmailAddressTable()->saveEmailAddress($emailAddress);
                 $arrId[] = $emailAddress->getId();
             }
             $this->getEmailAddressTable()->deleteRemovedEmailAddresses($client->getId(), $arrId);
             $arrId = array();
             foreach ($client->getPostAddresses() as $postAddress) {
                 $postAddress->setOwner($client->getId());
                 $this->getPostAddressTable()->savePostAddress($postAddress);
                 $arrId[] = $postAddress->getId();
             }
             $this->getPostAddressTable()->deleteRemovedPostAddresses($client->getId(), $arrId);
             // Redirect to list of clients
             return $this->redirect()->toRoute('mvitclient');
         }
     }
     return array('id' => $id, 'form' => $form);
 }