/** * Persists all entities managed by the repository and all cascading dependencies * * @return void */ public function persistEntities() { foreach ($this->entityManager->getUnitOfWork()->getIdentityMap() as $className => $entities) { if ($className === $this->entityClassName) { foreach ($entities as $entityToPersist) { $this->entityManager->flush($entityToPersist); } return; } } }
/** * {@inheritDoc} */ public function onChangeDecision(SluggableAdapter $ea, array &$config, $object, &$slug, &$needToChangeSlug) { $this->om = $ea->getObjectManager(); $isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object); $this->usedOptions = $config['handlers'][get_called_class()]; if (!isset($this->usedOptions['separator'])) { $this->usedOptions['separator'] = self::SEPARATOR; } if (!$isInsert && !$needToChangeSlug) { $changeSet = $ea->getObjectChangeSet($this->om->getUnitOfWork(), $object); if (isset($changeSet[$this->usedOptions['relationField']])) { $needToChangeSlug = true; } } }
/** * {@inheritDoc} */ public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug) { if (!$this->isInsert) { $options = $this->getOptions($object); $wrapped = AbstractWrapper::wrapp($object, $this->om); $meta = $wrapped->getMetadata(); $extConfig = $this->sluggable->getConfiguration($this->om, $meta->name); $config['useObjectClass'] = $extConfig['useObjectClass']; $target = $wrapped->getPropertyValue($config['slug']); $config['pathSeparator'] = $options['separator']; $ea->replaceRelative($object, $config, $target.$options['separator'], $slug); $uow = $this->om->getUnitOfWork(); // update in memory objects foreach ($uow->getIdentityMap() as $className => $objects) { // for inheritance mapped classes, only root is always in the identity map if ($className !== $wrapped->getRootObjectName()) { continue; } foreach ($objects as $object) { if (property_exists($object, '__isInitialized__') && !$object->__isInitialized__) { continue; } $oid = spl_object_hash($object); $objectSlug = $meta->getReflectionProperty($config['slug'])->getValue($object); if (preg_match("@^{$target}{$options['separator']}@smi", $objectSlug)) { $objectSlug = str_replace($target, $slug, $objectSlug); $meta->getReflectionProperty($config['slug'])->setValue($object, $objectSlug); $ea->setOriginalObjectProperty($uow, $oid, $config['slug'], $objectSlug); } } } } }
/** * @param $term * @throws \InvalidArgumentException * @return $this */ public function setTerm($term = null) { $this->initialize(); if (null === $term) { $this->entityTerm = null; return $this; } if (!$term instanceof Term) { throw new \InvalidArgumentException(sprintf('Expected instance of Term. "%s" given.', get_class($term))); } if ($this->em->getUnitOfWork()->getEntityState($term) == UnitOfWork::STATE_DETACHED) { $term = $this->em->merge($term); } if ($term->getVocabulary() !== $this->vocabulary) { throw new \InvalidArgumentException(sprintf('Term "%s" (#%d) does not belong in "%s" vocabulary.', $term->getName(), $term->getId(), $this->vocabulary->getName())); } if ($this->entityTerm && $this->entityTerm->getTerm() === $term) { return $this; } $className = $this->entityTerms->getClassName(); $this->setDirty(true); $eTerm = new $className(); /** @var $eTerm EntityTermInterface */ $eTerm->setTerm($term); $this->entityTerm = $eTerm; return $this; }
/** * Gives feedback if the persistence Manager has unpersisted changes. * * This is primarily used to inform the user if he tries to save * data in an unsafe request. * * @return boolean */ public function hasUnpersistedChanges() { $unitOfWork = $this->entityManager->getUnitOfWork(); if ($unitOfWork->getScheduledEntityInsertions() !== array() || $unitOfWork->getScheduledEntityUpdates() !== array() || $unitOfWork->getScheduledEntityDeletions() !== array() || $unitOfWork->getScheduledCollectionDeletions() !== array() || $unitOfWork->getScheduledCollectionUpdates() !== array()) { return TRUE; } return FALSE; }
/** * Gives feedback if the persistence Manager has unpersisted changes. * * This is primarily used to inform the user if he tries to save * data in an unsafe request. * * @return boolean */ public function hasUnpersistedChanges() { $unitOfWork = $this->entityManager->getUnitOfWork(); $unitOfWork->computeChangeSets(); if ($unitOfWork->getScheduledEntityInsertions() !== [] || $unitOfWork->getScheduledEntityUpdates() !== [] || $unitOfWork->getScheduledEntityDeletions() !== [] || $unitOfWork->getScheduledCollectionDeletions() !== [] || $unitOfWork->getScheduledCollectionUpdates() !== []) { return true; } return false; }
/** * {@inheritDoc} */ public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug) { $this->om = $ea->getObjectManager(); $isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object); if (!$isInsert) { $options = $config['handlers'][get_called_class()]; $wrapped = AbstractWrapper::wrapp($object, $this->om); $oldSlug = $wrapped->getPropertyValue($config['slug']); $mappedByConfig = $this->sluggable->getConfiguration($this->om, $options['relationClass']); if ($mappedByConfig) { $meta = $this->om->getClassMetadata($options['relationClass']); if (!$meta->isSingleValuedAssociation($options['mappedBy'])) { throw new InvalidMappingException("Unable to find " . $wrapped->getMetadata()->name . " relation - [{$options['mappedBy']}] in class - {$meta->name}"); } if (!isset($mappedByConfig['slugs'][$options['inverseSlugField']])) { throw new InvalidMappingException("Unable to find slug field - [{$options['inverseSlugField']}] in class - {$meta->name}"); } $mappedByConfig['slug'] = $mappedByConfig['slugs'][$options['inverseSlugField']]['slug']; $mappedByConfig['mappedBy'] = $options['mappedBy']; $ea->replaceInverseRelative($object, $mappedByConfig, $slug, $oldSlug); $uow = $this->om->getUnitOfWork(); // update in memory objects foreach ($uow->getIdentityMap() as $className => $objects) { // for inheritance mapped classes, only root is always in the identity map if ($className !== $mappedByConfig['useObjectClass']) { continue; } foreach ($objects as $object) { if (property_exists($object, '__isInitialized__') && !$object->__isInitialized__) { continue; } $oid = spl_object_hash($object); $objectSlug = $meta->getReflectionProperty($mappedByConfig['slug'])->getValue($object); if (preg_match("@^{$oldSlug}@smi", $objectSlug)) { $objectSlug = str_replace($oldSlug, $slug, $objectSlug); $meta->getReflectionProperty($mappedByConfig['slug'])->setValue($object, $objectSlug); $ea->setOriginalObjectProperty($uow, $oid, $mappedByConfig['slug'], $objectSlug); } } } } } }
/** * Checks if object has identifier already in unit of work. * * @param $reference * * @return bool */ private function hasIdentifier($reference) { // in case if reference is set after flush, store its identity $uow = $this->manager->getUnitOfWork(); if ($this->manager instanceof PhpcrDocumentManager) { return $uow->contains($reference); } else { return $uow->isInIdentityMap($reference); } }
/** * Schedule update of the owner in the unit of work */ private function changed() { if (null === $this->manager) { $this->manager = $this->registry->getManagerForClass(ClassUtils::getClass($this->owner)); } $uow = $this->manager->getUnitOfWork(); if (UnitOfWork::STATE_MANAGED === $uow->getDocumentState($this->owner) && !$uow->isScheduledForUpdate($this->owner)) { $uow->scheduleForUpdate($this->owner); } }
/** * send created event. * * @param $version */ public function sendEvent($version) { $entityInsertions = $this->manager->getUnitOfWork()->getScheduledEntityInsertions(); foreach ($entityInsertions as $entityInsertion) { $this->dispatchEvent($entityInsertion, Events::PRE_CREATE, $version); } $this->manager->getUnitOfWork()->computeChangeSets(); $entityUpdateds = $this->manager->getUnitOfWork()->getScheduledEntityUpdates(); foreach ($entityUpdateds as $entityUpdated) { $this->dispatchEvent($entityUpdated, Events::PRE_UPDATE, $version); } }
/** * {@inheritDoc} */ public function onChangeDecision(SluggableAdapter $ea, $slugFieldConfig, $object, &$slug, &$needToChangeSlug) { $this->om = $ea->getObjectManager(); $isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object); if (!$isInsert && !$needToChangeSlug) { $changeSet = $ea->getObjectChangeSet($this->om->getUnitOfWork(), $object); $options = $this->getOptions($object); if (isset($changeSet[$options['relationField']])) { $needToChangeSlug = true; } } }
/** * @param $element * @throws \InvalidArgumentException */ protected function checkType($element) { if (!$element instanceof Term) { throw new \InvalidArgumentException(sprintf('Expected instance of %s. "%s" given.', __NAMESPACE__ . '\\Term', get_class($element))); } if ($this->em->getUnitOfWork()->getEntityState($element) == UnitOfWork::STATE_DETACHED) { $element = $this->em->merge($element); } if ($element->getVocabulary() !== $this->vocabulary) { throw new \InvalidArgumentException(sprintf('Term "%s" (#%d) does not belong in the "%s" vocabulary.', $element->getName(), $element->getId(), $this->vocabulary->getName())); } }
/** * Loads an object using stored reference * named by $name * * @param string $name * @return object */ public function getReference($name) { $reference = $this->references[$name]; $meta = $this->manager->getClassMetadata(get_class($reference)); $uow = $this->manager->getUnitOfWork(); if (!$uow->isInIdentityMap($reference) && isset($this->identities[$name])) { $reference = $this->manager->getReference($meta->name, $this->identities[$name]); $this->references[$name] = $reference; // allready in identity map } return $reference; }
/** * Save an entity * * @param object $node * @param boolean $andFlush */ protected function saveEntity($entity, $andFlush = true) { if ($this->objectManager->getUnitOfWork()->getEntityState($entity) == \Doctrine\ORM\UnitOfWork::STATE_NEW) { return $this->createEntity($entity, $andFlush); } else { $em = $this->objectManager; $em->persist($entity); if ($andFlush) { $em->flush(); } } }
/** * Persists all entities managed by the repository and all cascading dependencies * * @return void */ public function persistEntities() { foreach ($this->entityManager->getUnitOfWork()->getIdentityMap() as $className => $entities) { if ($className === $this->entityClassName) { foreach ($entities as $entityToPersist) { $this->entityManager->flush($entityToPersist); } $this->emitRepositoryObjectsPersisted(); break; } } }
/** * Update product fields when a value is updated (ORM specific) * * @param ObjectManager $manager * @param ProductInterface $product * @param array $fields */ protected function updateProductFields(ObjectManager $manager, ProductInterface $product, $fields) { $uow = $manager->getUnitOfWork(); // ORM specific, for Document the value is embedded if (method_exists($uow, 'scheduleExtraUpdate')) { $now = new \DateTime('now', new \DateTimeZone('UTC')); $changes = array(); foreach ($fields as $field) { $changes[$field] = array(null, $now); } $uow->scheduleExtraUpdate($product, $changes); } }
/** * 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; } }
/** * 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)); $uow = $this->manager->getUnitOfWork(); if (!$uow->isInIdentityMap($reference) && isset($this->identities[$name])) { $reference = $this->manager->getReference($meta->name, $this->identities[$name]); $this->references[$name] = $reference; // already in identity map } return $reference; }
/** * {@inheritdoc} */ protected function releaseTreeLocks(ObjectManager $om, AdapterInterface $ea) { $uow = $om->getUnitOfWork(); foreach ($this->rootsOfTreesWhichNeedsLocking as $oid => $root) { $meta = $om->getClassMetadata(get_class($root)); $config = $this->listener->getConfiguration($om, $meta->name); $lockTimeProp = $meta->getReflectionProperty($config['lock_time']); $lockTimeProp->setAccessible(true); $lockTimeValue = null; $lockTimeProp->setValue($root, $lockTimeValue); $changes = array($config['lock_time'] => array(null, null)); $ea->recomputeSingleObjectChangeSet($uow, $meta, $root); unset($this->rootsOfTreesWhichNeedsLocking[$oid]); } }
/** * @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); } } }
private function getPluginScheduledForInsert(ObjectManager $om, $vendorName, $bundleName) { $result = null; $scheduledForInsert = $om->getUnitOfWork()->getScheduledEntityInsertions(); foreach ($scheduledForInsert as $entity) { if (get_class($entity) === 'Claroline\\CoreBundle\\Entity\\Plugin' && $entity->getVendorName() === $vendorName && $entity->getBundleName() === $bundleName) { $result = $entity; break; } } return $result; }
/** * Recomputes change set for Doctrine implementations not doing it automatically after the event. * * @param ObjectManager $om * @param UserInterface $user */ private function recomputeChangeSet(ObjectManager $om, UserInterface $user) { $meta = $om->getClassMetadata(get_class($user)); if ($om instanceof EntityManager) { $om->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $user); return; } if ($om instanceof DocumentManager) { $om->getUnitOfWork()->recomputeSingleDocumentChangeSet($meta, $user); } }
private function onEvent(\Doctrine\Common\Persistence\ObjectManager $om, $object, $event) { $className = get_class($object); if (!isset($this->configs[$className])) { return; } $conf =& $this->configs[$className]; /** * @var $meta \Doctrine\ORM\Mapping\ClassMetadata */ if (self::preUpdate === $event) { if (isset($conf['uuid'])) { $uow = $om->getUnitOfWork(); $meta = $om->getClassMetadata($className); foreach ($conf['uuid'] as $property_name => $property_conf) { $property = $meta->getReflectionProperty($property_name); $oldValue = $property->getValue($object); if (null === $oldValue) { $newValue = $this->uuid_generator->generate($om, $object); $property->setValue($object, $newValue); if ($object instanceof NotifyPropertyChanged) { $uow->propertyChanged($object, $property_name, $oldValue, $newValue); } } } } } else { if (self::onFlush === $event) { } else { if (self::postUpdate === $event) { if (isset($conf['file'])) { $meta = $om->getClassMetadata($className); $object_id = $meta->getReflectionProperty($conf['id'])->getValue($object); foreach ($conf['file'] as $property_name => $property_conf) { $property = $meta->getReflectionProperty($property_name); $oldValue = $property->getValue($object); if ($oldValue) { if ($object_id !== $oldValue->getEntityId()) { $oldValue->setEntityId($object_id); $om->persist($oldValue); $this->post_flush_persist_counter++; } } } } if (isset($conf['html'])) { $meta = $om->getClassMetadata($className); $object_id = $meta->getReflectionProperty($conf['id'])->getValue($object); $repo = $om->getRepository('Symforce\\AdminBundle\\Entity\\File'); foreach ($conf['html'] as $property_name => $property_conf) { $property = $meta->getReflectionProperty($property_name); $oldValue = $property->getValue($object); if ($oldValue) { preg_match_all($this->getRichTextFilesPattern(), $oldValue, $ms, PREG_SET_ORDER); if ($ms) { foreach ($ms as $ma) { $file = $repo->loadByUUID($ma[1]); if ($file && $file->getIsHtmlFile() && $object_id !== $file->getEntityId()) { $file->setEntityId($object_id); $om->persist($file); $this->post_flush_persist_counter++; } } } } } } } else { if (self::preRemove === $event) { if (isset($conf['file'])) { $meta = $om->getClassMetadata($className); $object_id = $meta->getReflectionProperty($conf['id'])->getValue($object); foreach ($conf['file'] as $property_name => $property_conf) { $property = $meta->getReflectionProperty($property_name); $oldValue = $property->getValue($object); if ($oldValue && $object_id === $oldValue->getEntityId() && $className === $oldValue->getClassName() && $property_name === $oldValue->getPropertyName() && $object_id === $oldValue->getEntityId()) { $om->remove($oldValue); } } } if (isset($conf['html'])) { $meta = $om->getClassMetadata($className); $object_id = $meta->getReflectionProperty($conf['id'])->getValue($object); $repo = $om->getRepository('Symforce\\AdminBundle\\Entity\\File'); foreach ($conf['html'] as $property_name => $property_conf) { $property = $meta->getReflectionProperty($property_name); $oldValue = $property->getValue($object); if ($oldValue) { preg_match_all($this->getRichTextFilesPattern(), $oldValue, $ms, PREG_SET_ORDER); if ($ms) { foreach ($ms as $ma) { $file = $repo->loadByUUID($ma[1]); if ($file && $file->getIsHtmlFile() && $className === $file->getClassName() && $property_name === $file->getPropertyName() && $object_id === $file->getEntityId()) { $om->remove($file); } } } } } } } } } } }