public function editAction()
 {
     $this->setAccess('frontend/squads/create');
     $squadID = (int) $this->params('id', null);
     $squadRepo = $this->getEntityManager()->getRepository('Frontend\\Squads\\Entity\\Squad');
     $squadEntity = $squadRepo->findOneBy(array('user' => $this->identity(), 'id' => $squadID));
     if (!$squadEntity) {
         $this->flashMessenger()->addErrorMessage('Squad not found');
         return $this->redirect()->toRoute('frontend/user/squads');
     }
     $form = new Member();
     $form->setEntityManager($this->getEntityManager());
     $form->init($squadEntity);
     if ($this->getRequest()->isPost()) {
         // zf bug workaround - deleting all members
         $postedData = (array) $this->getRequest()->getPost();
         if (!isset($postedData['members'])) {
             $postedData['members'] = array();
         }
         $form->setData($postedData);
         if ($form->isValid()) {
             $this->getEntityManager()->flush();
             $this->flashMessenger()->addSuccessMessage('Members successfully edited!');
             return $this->redirect()->refresh();
         } else {
             $form->populateValues($this->getRequest()->getPost());
         }
     }
     $viewModel = new ViewModel();
     $viewModel->setTemplate('/squads/member/edit.phtml');
     $viewModel->setVariable('form', $form);
     $viewModel->setVariable('squad', $squadEntity);
     return $viewModel;
 }
 /**
  * Create a new Squad Member
  *
  * @return ApiResponse
  */
 public function create()
 {
     $squadId = $this->params('id', 0);
     if ($squadId <= 0) {
         return new ApiResponse(null, 'missing squad parameter id', 404);
     }
     $squadRepository = $this->getEntityManager()->getRepository('Frontend\\Squads\\Entity\\Squad');
     /** @var \Frontend\Squads\Entity\Squad $squad */
     $squad = $squadRepository->findOneBy(array('user' => $this->getApiIdentity(), 'id' => $squadId), array('id' => 'desc'));
     if (!$squad) {
         return new ApiResponse(null, 'squad not found', 404);
     }
     $postData = $this->getArrayPostData();
     $memberForm = new Member();
     $memberForm->setEntityManager($this->getEntityManager());
     $memberForm->init(new \Frontend\Squads\Entity\Member());
     $memberForm->setData($postData);
     if ($memberForm->isValid()) {
         /** @var \Frontend\Squads\Entity\Member $member */
         $member = $memberForm->getData();
         $member->setSquad($squad);
         try {
             $this->getEntityManager()->persist($member);
             $this->getEntityManager()->flush($member);
         } catch (\Exception $e) {
             /** @todo Member UUID + Username allready exists validator */
             return new ApiResponse(null, array('uuid' => 'allready exists', 'username' => 'allready exists'), 422);
         }
         // save
         return new ApiResponse($member->getArrayCopy(), null, 201);
     } else {
         return new ApiResponse(null, $memberForm->getMessages(), 422);
     }
 }