Last active
December 18, 2015 18:39
-
-
Save TomHAnderson/5827533 to your computer and use it in GitHub Desktop.
Controller with entity returning associations as part of getArrayCopy(); create, edit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Controller; | |
use Zend\Mvc\Controller\AbstractActionController; | |
use Zend\View\Model\ViewModel; | |
use Zend\Form\Annotation\AnnotationBuilder; | |
use DbLoadCd\Entity\Test as TestEntity; | |
use Application\Service\Security; | |
class TestController extends AbstractActionController | |
{ | |
public function indexAction() | |
{ | |
$page = (int)$this->getEvent()->getRouteMatch()->getParam('page'); | |
$viewModel = new ViewModel(); | |
$viewModel->setVariable('page', $page); | |
return $viewModel; | |
} | |
public function createAction() | |
{ | |
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); | |
$id = $this->getEvent()->getRouteMatch()->getParam('projectId'); | |
$repository = $em->getRepository('DbLoadCd\Entity\Project'); | |
$user = $this->AppleConnectAuthentication()->getIdentityEntity(); | |
$project = $repository->find($id); | |
if (!Security::create($project)) { | |
throw new \BjyAuthorize\Exception\UnAuthorizedException('You do not have permission to create tests for this project.'); | |
} | |
$test = new TestEntity(); | |
$builder = new AnnotationBuilder(); | |
$form = $builder->createForm($test); | |
$viewModel = new ViewModel(); | |
$form->get('owner')->setValueOptions( | |
$this->doctrineSelectOptions( | |
'AppleConnect\Entity\User', | |
[], | |
['nickName' => 'ASC'], | |
['Application\Service\Security', 'view'] | |
) | |
); | |
$form->get('operator')->setValueOptions( | |
$this->doctrineSelectOptions( | |
'AppleConnect\Entity\User', | |
[], | |
['nickName' => 'ASC'], | |
['Application\Service\Security', 'view'] | |
) | |
); | |
$form->get('testType')->setValueOptions( | |
$this->doctrineSelectOptions( | |
'DbLoadCd\Entity\TestType', | |
[], | |
['name' => 'ASC'], | |
['Application\Service\Security', 'view'] | |
) | |
); | |
$form->get('testStatus')->setValueOptions( | |
$this->doctrineSelectOptions( | |
'DbLoadCd\Entity\TestStatus', | |
[], | |
['name' => 'ASC'], | |
['Application\Service\Security', 'view'] | |
) | |
); | |
$form->get('testResult')->setValueOptions( | |
$this->doctrineSelectOptions( | |
'DbLoadCd\Entity\TestResult', | |
[], | |
['name' => 'ASC'], | |
['Application\Service\Security', 'view'] | |
) | |
); | |
if ($this->getRequest()->isPost()) { | |
$form->setData($this->getRequest()->getPost()->toArray()); | |
$form->setUseInputFilterDefaults(false); | |
$form->setInputFilter($test->getInputFilter()); | |
if ($form->isValid()) { | |
$data = $form->getData(); | |
$data['project'] = $project; | |
$data['owner'] = $em->getRepository('AppleConnect\Entity\User')->find($data['owner']); | |
$data['operator'] = $em->getRepository('AppleConnect\Entity\User')->find($data['operator']); | |
$data['testType'] = $em->getRepository('DbLoadCd\Entity\TestType')->find($data['testType']); | |
$data['testStatus'] = $em->getRepository('DbLoadCd\Entity\TestStatus')->find($data['testStatus']); | |
$data['testResult'] = $em->getRepository('DbLoadCd\Entity\TestResult')->find($data['testResult']); | |
$test->exchangeArray($data); | |
$em->persist($test); | |
$em->flush(); | |
return $this->plugin('redirect')->toRoute('test/detail', array( | |
'testId' => $test->getId(), | |
)); | |
} | |
} | |
$viewModel->setVariable('form', $form); | |
return $viewModel; | |
} | |
public function detailAction() | |
{ | |
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); | |
$id = (int)$this->getEvent()->getRouteMatch()->getParam('testId'); | |
$repository = $em->getRepository('DbLoadCd\Entity\Test'); | |
$test = $repository->find($id); | |
if (!Security::view($test)) { | |
throw new \BjyAuthorize\Exception\UnAuthorizedException('You do not have permission to view this test.'); | |
} | |
return array( | |
'test' => $test | |
); | |
} | |
public function editAction() | |
{ | |
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); | |
$id = (int)$this->getEvent()->getRouteMatch()->getParam('testId'); | |
$repository = $em->getRepository('DbLoadCd\Entity\Test'); | |
$test = $repository->find($id); | |
if (!Security::edit($test)) { | |
throw new \BjyAuthorize\Exception\UnAuthorizedException('You do not have permission to view this test.'); | |
} | |
$builder = new AnnotationBuilder(); | |
$form = $builder->createForm($test); | |
$form->get('owner')->setValueOptions( | |
$this->doctrineSelectOptions( | |
'AppleConnect\Entity\User', | |
[], | |
['nickName' => 'ASC'], | |
['Application\Service\Security', 'view'] | |
) | |
); | |
$form->get('operator')->setValueOptions( | |
$this->doctrineSelectOptions( | |
'AppleConnect\Entity\User', | |
[], | |
['nickName' => 'ASC'], | |
['Application\Service\Security', 'view'] | |
) | |
); | |
$form->get('testType')->setValueOptions( | |
$this->doctrineSelectOptions( | |
'DbLoadCd\Entity\TestType', | |
[], | |
['name' => 'ASC'], | |
['Application\Service\Security', 'view'] | |
) | |
); | |
$form->get('testStatus')->setValueOptions( | |
$this->doctrineSelectOptions( | |
'DbLoadCd\Entity\TestStatus', | |
[], | |
['name' => 'ASC'], | |
['Application\Service\Security', 'view'] | |
) | |
); | |
$form->get('testResult')->setValueOptions( | |
$this->doctrineSelectOptions( | |
'DbLoadCd\Entity\TestResult', | |
[], | |
['name' => 'ASC'], | |
['Application\Service\Security', 'view'] | |
) | |
); | |
$data = $test->getArrayCopy(); | |
$data['owner'] = ($data['owner'] instanceof \AppleConnect\Entity\User) ? $data['owner']->getId(): null; | |
$data['operator'] = ($data['operator'] instanceof \AppleConnect\Entity\User) ? $data['operator']->getId(): null; | |
$data['testType'] = ($data['testType'] instanceof \DbLoadCd\Entity\TestType) ? $data['testType']->getId(): null; | |
$data['testStatus'] = ($data['testStatus'] instanceof \DbLoadCd\Entity\TestStatus) ? $data['testStatus']->getId(): null; | |
$data['testResult'] = ($data['testResult'] instanceof \DbLoadCd\Entity\TestResult) ? $data['testResult']->getId(): null; | |
$form->setData($data); | |
if ($this->getRequest()->isPost()) { | |
$form->setData($this->getRequest()->getPost()->toArray()); | |
$form->setUseInputFilterDefaults(false); | |
$form->setInputFilter($test->getInputFilter()); | |
if ($form->isValid()) { | |
$data = $form->getData(); | |
$data['project'] = $test->getProject(); | |
$data['owner'] = $em->getRepository('AppleConnect\Entity\User')->find($data['owner']); | |
$data['operator'] = $em->getRepository('AppleConnect\Entity\User')->find($data['operator']); | |
$data['testType'] = $em->getRepository('DbLoadCd\Entity\TestType')->find($data['testType']); | |
$data['testStatus'] = $em->getRepository('DbLoadCd\Entity\TestStatus')->find($data['testStatus']); | |
$data['testResult'] = $em->getRepository('DbLoadCd\Entity\TestResult')->find($data['testResult']); | |
$test->exchangeArray($data); | |
$em->flush(); | |
return $this->plugin('redirect')->toRoute('test/detail', array( | |
'testId' => $test->getId(), | |
)); | |
} | |
} | |
$viewModel = new ViewModel(); | |
$viewModel->setVariable('form', $form); | |
return $viewModel; | |
} | |
public function deleteAction() | |
{ | |
throw new \Exception('not implemented'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment