public function addAction() { /** @var \Zend\Http\Request $request */ $request = $this->getRequest(); $sl = $this->getServiceLocator(); /* @var $cityMapper \Address\Model\CityMapper */ $cityMapper = $sl->get('Address\\Model\\CityMapper'); $city = new \Address\Model\City(); /* @var $districtMapper \Address\Model\DistrictMapper */ $districtMapper = $sl->get('Address\\Model\\DistrictMapper'); $district = new \Address\Model\District(); $districts = array(); if (!!($cityId = $request->getPost('cityId'))) { $district->setCityId($cityId); $districts = $districtMapper->fetchAll($district); } $form = new \System\Form\User\Add($this->getServiceLocator()); $form->setCities($city->toSelectBoxArray($cityMapper->fetchAll())); $form->setDistricts($district->toSelectBoxArray($districts)); if ($this->getRequest()->isPost()) { $data = $this->getRequest()->getPost(); $form->setData($data); if ($form->isValid()) { $user = new User(); $user->exchangeArray($form->getData()); $user->setSalt($user->generateSalt()); $user->setPassword($user->createPassword()); if (!$user->getRole()) { $user->setRole(User::ROLE_GUEST); } if ($user->getBirthdate()) { $user->setBirthdate(DateBase::toCommonDate($user->getBirthdate())); } $user->setActive(1); $user->setCreatedById($this->user()->getIdentity()); $user->setCreatedDate(DateBase::getCurrentDate()); $user->setCreatedDateTime(DateBase::getCurrentDateTime()); $userMapper = $this->getServiceLocator()->get('\\User\\Model\\UserMapper'); $userMapper->save($user); if ($form->get('afterSubmit')->getValue()) { return $this->redirect()->toUrl($form->get('afterSubmit')->getValue()); } } } $viewModel = new ViewModel(); $viewModel->setVariable('form', $form); return $viewModel; }