/** * {@inheritdoc} */ protected function doUpdate($object, $flush = true) { if (!$this->manager->contains($object)) { $this->manager->persist($object); } if ($flush) { $this->flush($object); } }
/** * {@inheritdoc} */ public function handle(ApiCallInterface $apiCall) { if (!$this->objectManager->contains($apiCall)) { if ($apiCall->getId() === null) { $this->objectManager->persist($apiCall); } else { $this->objectManager->merge($apiCall); } } $this->objectManager->flush(); }
/** * {@inheritdoc} */ public function update(Content $content) { $managed = true; if (!$this->om->contains($content)) { $managed = false; $content = $this->om->merge($content); } $this->om->flush($content); if (!$managed) { $this->om->detach($content); } }
/** * Returns the values of the identifier fields of an entity. * * Doctrine must know about this entity, that is, the entity must already * be persisted or added to the identity map before. Otherwise an * exception is thrown. * * @param object $entity The entity for which to get the identifier * * @return array The identifier values * * @throws FormException If the entity does not exist in Doctrine's identity map */ public function getIdentifierValues($entity) { if (!$this->em->contains($entity)) { throw new FormException('Entities passed to the choice field must be managed'); } return $this->classMetadata->getIdentifierValues($entity); }
function it_can_handle_exiting_unmanaged_objects(ObjectManager $objectManager, ApiCallInterface $apiCall) { $objectManager->contains($apiCall)->willReturn(false); $apiCall->getId()->willReturn(1); $objectManager->merge($apiCall)->shouldBeCalled(); $objectManager->flush()->shouldBeCalled(); $this->handle($apiCall); }
/** * Returns the values of the identifier fields of an entity. * * Doctrine must know about this entity, that is, the entity must already * be persisted or added to the identity map before. Otherwise an * exception is thrown. * * @param object $entity The entity for which to get the identifier * * @return array The identifier values * * @throws Exception If the entity does not exist in Doctrine's identity map */ private function getIdentifierValues($entity) { if (!$this->em->contains($entity)) { throw new Exception('Entities passed to the choice field must be managed. Maybe ' . 'persist them in the entity manager?'); } $this->em->initializeObject($entity); return $this->classMetadata->getIdentifierValues($entity); }
/** * Returns the ID value for an object. * * This method assumes that the object has a single-column ID. * * @param object $object The object. * * @return mixed The ID value. */ public function getIdValue($object) { if (!$object) { return; } if (!$this->om->contains($object)) { throw new RuntimeException('Entities passed to the choice field must be managed. Maybe ' . 'persist them in the entity manager?'); } $this->om->initializeObject($object); return current($this->classMetadata->getIdentifierValues($object)); }
/** * Loads an object using stored reference * named by $name * * @param string $name * @throws OutOfBoundsException - if repository does not exist * @return object */ public function getReference($name) { if (!$this->hasReference($name)) { throw new \OutOfBoundsException("Reference to: ({$name}) does not exist"); } $reference = $this->references[$name]; $meta = $this->manager->getClassMetadata(get_class($reference)); if (!$this->manager->contains($reference) && isset($this->identities[$name])) { $reference = $this->manager->getReference($meta->name, $this->identities[$name]); $this->references[$name] = $reference; // already in identity map } return $reference; }
/** * Returns the (internal) identifier for the object, if it is known to the * backend. Otherwise NULL is returned. * * Note: this returns an identifier even if the object has not been * persisted in case of AOP-managed entities. Use isNewObject() if you need * to distinguish those cases. * * @param object $object * @return mixed The identifier for the object if it is known, or NULL * @api * @todo improve try/catch block */ public function getIdentifierByObject($object) { if ($this->entityManager->contains($object)) { try { return current($this->entityManager->getUnitOfWork()->getEntityIdentifier($object)); } catch (\Doctrine\ORM\ORMException $e) { return NULL; } } elseif (property_exists($object, 'FLOW3_Persistence_Identifier')) { return \TYPO3\FLOW3\Reflection\ObjectAccess::getProperty($object, 'FLOW3_Persistence_Identifier', TRUE); } else { return NULL; } }
/** * Returns the (internal) identifier for the object, if it is known to the * backend. Otherwise NULL is returned. * * Note: this returns an identifier even if the object has not been * persisted in case of AOP-managed entities. Use isNewObject() if you need * to distinguish those cases. * * @param object $object * @return mixed The identifier for the object if it is known, or NULL * @api * @todo improve try/catch block */ public function getIdentifierByObject($object) { if (property_exists($object, 'Persistence_Object_Identifier')) { $identifierCandidate = ObjectAccess::getProperty($object, 'Persistence_Object_Identifier', true); if ($identifierCandidate !== null) { return $identifierCandidate; } } if ($this->entityManager->contains($object)) { try { return current($this->entityManager->getUnitOfWork()->getEntityIdentifier($object)); } catch (\Doctrine\ORM\ORMException $exception) { } } return null; }
/** * Returns the (internal) identifier for the object, if it is known to the * backend. Otherwise NULL is returned. * * Note: this returns an identifier even if the object has not been * persisted in case of AOP-managed entities. Use isNewObject() if you need * to distinguish those cases. * * @param object $object * @return mixed The identifier for the object if it is known, or NULL * @api * @todo improve try/catch block */ public function getIdentifierByObject($object) { if (property_exists($object, 'Persistence_Object_Identifier')) { $identifierCandidate = \TYPO3\Flow\Reflection\ObjectAccess::getProperty($object, 'Persistence_Object_Identifier', TRUE); if ($identifierCandidate !== NULL) { return $identifierCandidate; } } if ($this->entityManager->contains($object)) { try { return current($this->entityManager->getUnitOfWork()->getEntityIdentifier($object)); } catch (\Doctrine\ORM\ORMException $e) { } } return NULL; }
/** * (non-PHPdoc) * * @see \Symfony\Component\Security\Core\User\UserProviderInterface::refreshUser() */ public function refreshUser(UserInterface $user) { $userClass = get_class($user); if (!$this->supportsClass($userClass)) { throw new UnsupportedUserException("Unsupported user '" . $userClass . "'"); } /* @var ADH\UserBundle\Entity\User $user */ if ($this->entityManager->contains($user)) { $this->entityManager->refresh($user); } else { $user = $this->userRepository->findOneById($user->getId()); } if ($user === null) { throw new UsernameNotFoundException("User does not exist."); } return $user; }
/** * {@inheritDoc} */ public function supports($object) { return $this->om->contains($object); }
/** * @param ObjectManager|DocumentManager|EntityManager $om * @param object $object */ protected function recomputeSingleObjectChangeSet(ObjectManager $om, $object) { if ($om->contains($object)) { $classMetadata = $om->getClassMetadata(get_class($object)); $uow = $om->getUnitOfWork(); if ($uow instanceof ODMUnitOfWork) { $uow->recomputeSingleDocumentChangeSet($classMetadata, $object); } elseif ($uow instanceof ORMUnitOfWork) { $uow->recomputeSingleEntityChangeSet($classMetadata, $object); } } }
/** * @param object $entity entity * @param ObjectManager $entityManager entityManager * @param UnitOfWork $uow uow */ private function computeViolations($entity, ObjectManager $entityManager, UnitOfWork $uow) { if (!($handlers = $this->handlerManager->fetch($entity))) { return null; } $list = $this->violationManager->getViolationListNotFixed($entity); $list->setFixed(true); foreach ($handlers as $handler) { $handler->validate($entity, $list); } $uow->computeChangeSets(); foreach ($list as $violation) { $changeSet = $uow->getEntityChangeSet($violation); if (!empty($changeSet) || !$entityManager->contains($violation)) { $this->violations[spl_object_hash($violation)] = $violation; } } }
/** * This method cleans empty translation entities. * * @param mixed $collection * @param ClassMetadata $classMetadata */ private function cleanupCollection(&$collection, $classMetadata) { $notNullableStringProperties = array(); $nullableStringProperties = array(); $otherProperties = array(); foreach ($classMetadata->getFieldNames() as $fieldName) { $fieldMapping = $classMetadata->getFieldMapping($fieldName); // skip id field if (isset($fieldMapping['id']) && $fieldMapping['id'] === true) { continue; } // skip language field if (in_array($fieldMapping['fieldName'], $this->autoRemoveIgnoreFields) || $fieldMapping['fieldName'] === $this->localePropertyPath) { continue; } if ($fieldMapping['nullable']) { if ($fieldMapping['type'] === 'string') { $nullableStringProperties[] = $fieldMapping['fieldName']; } else { $otherProperties[] = $fieldMapping['fieldName']; } } else { if ($fieldMapping['type'] === 'string') { $notNullableStringProperties[] = $fieldMapping['fieldName']; } else { $otherProperties[] = $fieldMapping['fieldName']; } } } $toRemove = array(); foreach ($collection as $entity) { // entity is translation, i.e. $sitemapNodeTranslation $allNullableStringsAreNull = true; foreach ($nullableStringProperties as $pp) { // $pp is property path, i.e. 'short_description' // this calls something like $sitemapNodeTranslation->getShortDescription() $value = $this->propertyAccessor->getValue($entity, $pp); if (trim($value) === '') { $this->propertyAccessor->setValue($entity, $pp, null); } else { $allNullableStringsAreNull = false; } } $allNotNullableStringsAreNull = true; foreach ($notNullableStringProperties as $pp) { $value = $this->propertyAccessor->getValue($entity, $pp); if ($value !== null) { if (trim($value) !== '') { $allNotNullableStringsAreNull = false; } } else { $this->propertyAccessor->setValue($entity, $pp, ''); } } $otherAreNull = true; foreach ($otherProperties as $pp) { $value = $this->propertyAccessor->getValue($entity, $pp); if ($value !== null) { $otherAreNull = false; } } if ($allNullableStringsAreNull && $allNotNullableStringsAreNull && $otherAreNull) { $toRemove[] = $entity; if (null !== $this->objectManager && $this->objectManager->contains($entity)) { $this->objectManager->remove($entity); // Delete from db. } } } $this->removeFromCollection($collection, $toRemove); }