/** * Processes the form and build the Response object * * @param Request $request * @param Organization $organization * @return Response|static * @throws BadRequestHttpException */ public function processForm(Request $request, Organization $organization) { $statusCode = !$this->em->contains($organization) ? 201 : 204; $form = $this->container->get('form.factory')->create(OrganizationType::class, $organization); $formData = json_decode($request->getContent(), true); $form->submit($this->prepareFormData($formData)); if (!$form->isValid()) { return View::create($form, 400); } if (!$this->em->contains($organization)) { $this->em->persist($organization); } try { $this->em->flush(); } catch (\Exception $e) { $pdoException = $e->getPrevious(); // unique constraint if ($pdoException->getCode() === '23000') { throw new BadRequestHttpException('Error 23000!'); } throw new BadRequestHttpException('Unknown error code: ' . $pdoException->getCode()); } $response = new Response(); $response->setStatusCode($statusCode); if (201 === $statusCode) { $response->headers->set('Access-Control-Expose-Headers', 'Location'); $response->headers->set('Location', $this->container->get('router')->generate('api_v1_get_organization', ['organization' => $organization->getId()], UrlGeneratorInterface::RELATIVE_PATH)); } return $response; }
/** * */ public function onClear(\Doctrine\Common\EventArgs $args) { foreach ($this->array as $entity) { if (!$this->entityManager->contains($entity)) { $entity->free(); } } }
/** * @param $entity */ private function persistEntity($entity) { if ($this->isEntity($entity)) { if (!$this->em->contains($entity)) { $this->em->persist($entity); $this->persistAllAssociations($entity); } } }
/** * @Given I have a valid :role access token * * @param $role * @param null $email */ public function iHaveAValidAccessToken($role, $email = null) { $user = $this->userFixtureContext->generateDefaultUser($role, $email); if (!$this->entityManager->contains($user)) { $this->entityManager->persist($user); } $this->entityManager->flush(); $token = $this->generateAccessToken($user); $this->setAuthorizationBearerHeader($token); }
/** * Savely persist Doctrine 2 Entity information * * @param ezcWorkflowExecution $execution * @param string $variableName * @param $value */ public function save(ezcWorkflowExecution $execution, $variableName, $value) { if (!is_object($value)) { return null; } if (!$this->em->contains($value)) { throw new \ezcWorkflowExecutionException("Entity '" . get_class($value) . "' at variable " . $variableName . " has to be managed by the EntityManager."); } $entityData = array(get_class($value), $this->em->getUnitOfWork()->getEntityIdentifier($value)); $entityDetailsVariable = $variableName . "_dc2entity"; $execution->setVariable($entityDetailsVariable, $entityData); $execution->setVariable($variableName, null); }
/** * Processes the form and build the Response object * * @param Request $request * @param Organization $department * @param Setting $setting * @return Response|static * @throws BadRequestHttpException */ public function processForm(Request $request, Organization $department, Setting $setting) { $statusCode = $this->em->contains($setting) ? 204 : 201; $form = $this->container->get('form.factory')->create(SettingType::class, $setting); $formData = json_decode($request->getContent(), true); $form->submit($this->prepareFormData($formData)); if (!$form->isValid()) { return View::create($form, 400); } if (!$this->em->contains($setting)) { $setting->setDepartment($department); $this->em->persist($setting); } try { $this->em->flush(); } catch (\Exception $e) { throw new BadRequestHttpException(); } $response = new Response(); $response->setStatusCode($statusCode); if (201 === $statusCode) { $response->headers->set('Access-Control-Expose-Headers', 'Location'); $response->headers->set('Location', $this->container->get('router')->generate('api_v1_get_organization_departments_settings', ['organization' => $department->getOrganization()->getId(), 'department' => $department->getId(), 'setting' => $setting->getId()], UrlGeneratorInterface::RELATIVE_PATH)); } return $response; }
/** * Saves the entity. * * @param object $entity * @return bool */ public function saveEntity($entity) { if (!$this->entityManager->contains($entity)) { $this->entityManager->persist($entity); } $this->entityManager->flush(); return true; }
/** * Returns the indexed value of an element. * * @param AbstractClassContent $content The content flushed. * @param string $indexedElement The parameter to index. * * @return array An array of the element value and the content owner. */ private static function getContentValue(AbstractClassContent $content, $indexedElement) { $value = $owner = $content; $elements = explode('->', $indexedElement); foreach ($elements as $element) { $value = $value->getData($element); if (!$value instanceof AbstractClassContent) { break; } if (!self::$em->contains($value)) { $value = self::$em->find(get_class($value), $value->getUid()); } $owner = $value; } return [$value, $owner]; }
public function testFindStartableJobDetachesNonStartableJobs() { $a = new Job('a'); $b = new Job('b'); $a->addDependency($b); $this->em->persist($a); $this->em->persist($b); $this->em->flush(); $this->assertTrue($this->em->contains($a)); $this->assertTrue($this->em->contains($b)); $excludedIds = array(); $startableJob = $this->repo->findStartableJob($excludedIds); $this->assertNotNull($startableJob); $this->assertEquals($b->getId(), $startableJob->getId()); $this->assertEquals(array($a->getId()), $excludedIds); $this->assertFalse($this->em->contains($a)); $this->assertTrue($this->em->contains($b)); }
/** * Determines whether an entity instance is managed in this EntityManager. * * @param object $entity * @return boolean TRUE if this EntityManager currently manages the given entity, FALSE otherwise. * @static */ public static function contains($entity) { return \Doctrine\ORM\EntityManager::contains($entity); }
/** * {@inheritdoc} */ public function contains($entity) { return $this->wrapped->contains($entity); }