Created
June 24, 2013 17:22
-
-
Save TomHAnderson/5851831 to your computer and use it in GitHub Desktop.
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 | |
// Step 1: create form through annotations | |
$test = new TestEntity(); | |
$builder = new AnnotationBuilder(); | |
$form = $builder->createForm($test); | |
$viewModel = new ViewModel(); | |
// Step 2: populate select elements | |
$form->get('owner')->setValueOptions( | |
$this->doctrineSelectOptions( | |
'AppleConnect\Entity\User', | |
[], | |
['nickName' => 'ASC'], | |
['Application\Service\Security', 'view'] | |
) | |
); | |
// Step 3: fetch data, compose selects. Annotations would remove this step but using them doesn't give us fine grained | |
// handling as seen in step 2 | |
$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); | |
// Step 4: when a form is submitted recompose the getArrayCopy to a exchangeArray | |
// Merge current entity data with form data so fields which may be required by exchangeArray will be populated with | |
// their current values and ignored by form data | |
$data = array_merge($test->getArrayCopy(), $form->getData()); | |
$data['owner'] = ($data['owner']) ? | |
$em->getRepository('AppleConnect\Entity\UserGroup')->find($data['owner']): | |
null; | |
$data['operator'] = ($data['operator']) ? | |
$em->getRepository('AppleConnect\Entity\UserGroup')->find($data['operator']): | |
null; | |
$data['testType'] = ($data['testType']) ? | |
$em->getRepository('DbLoadCd\Entity\TestType')->find($data['testType']): | |
null; | |
$data['testStatus'] = ($data['testStatus']) ? | |
$em->getRepository('DbLoadCd\Entity\TestStatus')->find($data['testStatus']): | |
null; | |
$data['testResult'] = ($data['testResult']) ? | |
$em->getRepository('DbLoadCd\Entity\TestResult')->find($data['testResult']): | |
null; | |
$test->exchangeArray($data); | |
$em->flush(); | |
// Step 5: field setters | |
// If an association can be set to null do not use type hinting in the setter. But you may check possible values | |
// manually | |
public function setStartDate($value) { | |
if (is_null($value) or $value instanceof \DateTime) { | |
$this->startDate = $value; | |
return $this; | |
} else { | |
throw new \Exception('Invalid Estimated Completion Date'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment