Executes all document upserts for documents of the specified type.

@param ClassMetadata $class
@param array         $documents Array of documents to upsert

@return void
322020
private function executeUpserts(ClassMetadata $class, array $documents)
    {
        $persister = $this->getDocumentPersister($class->name);

        foreach ($documents as $oid => $document) {
            try {
                $persister->upsert($document);
                $persister->handleCollections($document);

                unset($this->documentUpserts[$oid]);
            } catch (\Aws\DynamoDb\Exception\DynamoDbException $e) {
                unset($this->documentUpserts[$oid]);

                throw $e;
            }

            $this->lifecycleEventManager->postPersist($class, $document);
        }
    }
897443
public function setDocuments(array $documents)
    {
        if ($documents instanceof PagSeguroDocument) {
            $this->documents = $documents;
        } else {
            if (is_array($documents)) {
                $this->addDocument($documents['type'], $documents['value']);
            }
        }
    }
998674
public function postRemove(ClassMetadata $class, $document)
    {
        $class->invokeLifecycleCallbacks(Event::postRemove, $document, [new LifecycleEventArgs($document, $this->dm)]);
        $this->evm->dispatchEvent(Event::postRemove, new LifecycleEventArgs($document, $this->dm));
    }
59479
private function executeUpdates($documents, $dispatchEvents = true)
    {
        foreach ($documents as $oid => $document) {
            if (!$this->contains($oid)) {
                continue;
            }

            $class = $this->dm->getClassMetadata(get_class($document));
            $node = $this->session->getNode($this->getDocumentId($document));

            if ($this->writeMetadata) {
                $this->documentClassMapper->writeMetadata($this->dm, $node, $class->name);
            }

            if ($dispatchEvents) {
                if ($invoke = $this->eventListenersInvoker->getSubscribedSystems($class, Event::preUpdate)) {
                    $this->eventListenersInvoker->invoke(
                        $class,
                        Event::preUpdate,
                        $document,
                        new PreUpdateEventArgs($document, $this->dm, $this->documentChangesets[$oid]),
                        $invoke
                    );
                    $this->changesetComputed = array_diff($this->changesetComputed, [$oid]);
                    $this->computeChangeSet($class, $document);
                }
            }

            $fields = $this->documentChangesets[$oid]['fields'] ?? [];
            foreach ($fields as $fieldName => $data) {
                $fieldValue = $data[1];

                // PHPCR does not validate nullable unless we would start to
                // generate custom node types, which we at the moment don't.
                // the ORM can delegate this validation to the relational database
                // that is using a strict schema.
                // do this after the preUpdate events to give listener a last
                // chance to provide values
                if (null === $fieldValue
                    && in_array($fieldName, $class->fieldMappings) // only care about non-virtual fields
                    && !$class->isNullable($fieldName)
                    && !$this->isAutocreatedProperty($class, $fieldName)
                ) {
                    throw new PHPCRException(sprintf('Field "%s" of class "%s" is not nullable', $fieldName, $class->name));
                }

                // Ignore translatable fields (they will be persisted by the translation strategy)
                if (in_array($fieldName, $class->translatableFields)) {
                    continue;
                }

                $mapping = $class->mappings[$fieldName];
                if (in_array($fieldName, $class->fieldMappings)) {
                    $type = PropertyType::valueFromName($mapping['type']);
                    if ($mapping['multivalue']) {
                        $value = empty($fieldValue) ? null : ($fieldValue instanceof Collection ? $fieldValue->toArray() : $fieldValue);
                        if ($value && isset($mapping['assoc'])) {
                            $value = $this->processAssoc($node, $mapping, $value);
                        }
                    } else {
                        $value = $fieldValue;
                    }
                    $node->setProperty($mapping['property'], $value, $type);
                } elseif ($mapping['type'] === $class::MANY_TO_ONE
                    || $mapping['type'] === $class::MANY_TO_MANY
                ) {
                    if (!$this->writeMetadata) {
                        continue;
                    }
                    if ($node->hasProperty($mapping['property']) && is_null($fieldValue)) {
                        $node->getProperty($mapping['property'])->remove();
                        if (isset($mapping['assoc'])) {
                            $this->removeAssoc($node, $mapping);
                        }
                        continue;
                    }

                    switch ($mapping['strategy']) {
                        case 'hard':
                            $strategy = PropertyType::REFERENCE;
                            break;
                        case 'path':
                            $strategy = PropertyType::PATH;
                            break;
                        default:
                            $strategy = PropertyType::WEAKREFERENCE;
                            break;
                    }

                    if ($mapping['type'] === $class::MANY_TO_MANY) {
                        if (isset($fieldValue)) {
                            $refNodesIds = [];
                            foreach ($fieldValue as $fv) {
                                if ($fv === null) {
                                    continue;
                                }

                                $associatedNode = $this->session->getNode($this->getDocumentId($fv));
                                if ($strategy === PropertyType::PATH) {
                                    $refNodesIds[] = $associatedNode->getPath();
                                } else {
                                    $refClass = $this->dm->getClassMetadata(get_class($fv));
                                    $this->setMixins($refClass, $associatedNode, $fv);
                                    if (!$associatedNode->isNodeType('mix:referenceable')) {
                                        throw new PHPCRException(sprintf('Referenced document %s is not referenceable. Use referenceable=true in Document annotation: '.self::objToStr($document, $this->dm), ClassUtils::getClass($fv)));
                                    }
                                    $refNodesIds[] = $associatedNode->getIdentifier();
                                }
                            }

                            $refNodesIds = empty($refNodesIds) ? null : $refNodesIds;
                            $node->setProperty($mapping['property'], $refNodesIds, $strategy);
                        }
                    } elseif ($mapping['type'] === $class::MANY_TO_ONE) {
                        if (isset($fieldValue)) {
                            $associatedNode = $this->session->getNode($this->getDocumentId($fieldValue));

                            if ($strategy === PropertyType::PATH) {
                                $node->setProperty($fieldName, $associatedNode->getPath(), $strategy);
                            } else {
                                $refClass = $this->dm->getClassMetadata(get_class($fieldValue));
                                $this->setMixins($refClass, $associatedNode, $document);
                                if (!$associatedNode->isNodeType('mix:referenceable')) {
                                    throw new PHPCRException(sprintf('Referenced document %s is not referenceable. Use referenceable=true in Document annotation: '.self::objToStr($document, $this->dm), ClassUtils::getClass($fieldValue)));
                                }
                                $node->setProperty($mapping['property'], $associatedNode->getIdentifier(), $strategy);
                            }
                        }
                    }
                } elseif ('referrers' === $mapping['type']) {
                    if (isset($fieldValue)) {

                        /*
                         * each document in referrers field is supposed to
                         * reference this document, so we have to update its
                         * referencing property to contain the uuid of this
                         * document
                         */
                        foreach ($fieldValue as $fv) {
                            if ($fv === null) {
                                continue;
                            }

                            if (!$fv instanceof $mapping['referringDocument']) {
                                throw new PHPCRException(sprintf('%s is not an instance of %s for document %s field %s', self::objToStr($fv, $this->dm), $mapping['referencedBy'], self::objToStr($document, $this->dm), $mapping['fieldName']));
                            }

                            $referencingNode = $this->session->getNode($this->getDocumentId($fv));
                            $referencingMeta = $this->dm->getClassMetadata($mapping['referringDocument']);
                            $referencingField = $referencingMeta->getAssociation($mapping['referencedBy']);

                            $uuid = $node->getIdentifier();
                            $strategy = $referencingField['strategy'] == 'weak' ? PropertyType::WEAKREFERENCE : PropertyType::REFERENCE;
                            switch ($referencingField['type']) {
                                case ClassMetadata::MANY_TO_ONE:
                                    $ref = $referencingMeta->getFieldValue($fv, $referencingField['fieldName']);
                                    if ($ref !== null && $ref !== $document) {
                                        throw new PHPCRException(sprintf('Conflicting settings for referrer and reference: Document %s field %s points to %s but document %s has set first document as referrer on field %s', self::objToStr($fv, $this->dm), $referencingField['fieldName'], self::objToStr($ref, $this->dm), self::objToStr($document, $this->dm), $mapping['fieldName']));
                                    }
                                    // update the referencing document field to point to this document
                                    $referencingMeta->setFieldValue($fv, $referencingField['fieldName'], $document);
                                    // and make sure the reference is not deleted in this change because the field could be null
                                    unset($this->documentChangesets[spl_object_hash($fv)]['fields'][$referencingField['fieldName']]);
                                    // store the change in PHPCR
                                    $referencingNode->setProperty($referencingField['property'], $uuid, $strategy);
                                    break;
                                case ClassMetadata::MANY_TO_MANY:
                                    /** @var $collection ReferenceManyCollection */
                                    $collection = $referencingMeta->getFieldValue($fv, $referencingField['fieldName']);
                                    if ($collection instanceof PersistentCollection && $collection->isDirty()) {
                                        throw new PHPCRException(sprintf('You may not modify the reference and referrer collections of interlinked documents as this is ambiguous. Reference %s on document %s and referrers %s on document %s are both modified', self::objToStr($fv, $this->dm), $referencingField['fieldName'], self::objToStr($document, $this->dm), $mapping['fieldName']));
                                    }
                                    if ($collection) {
                                        // make sure the reference is not deleted in this change because the field could be null
                                        unset($this->documentChangesets[spl_object_hash($fv)]['fields'][$referencingField['fieldName']]);
                                    } else {
                                        $collection = new ReferenceManyCollection($this->dm, $fv, $referencingField['property'], [$node], $class->name, null, $this->getReferenceManyCollectionTypeFromMetadata($mapping));
                                        $referencingMeta->setFieldValue($fv, $referencingField['fieldName'], $collection);
                                    }

                                    if ($referencingNode->hasProperty($referencingField['property'])) {
                                        if (!in_array($uuid, $referencingNode->getProperty($referencingField['property'])->getString())) {
                                            if (!$collection instanceof PersistentCollection || !$collection->isDirty()) {
                                                // update the reference collection: add us to it
                                                $collection->add($document);
                                            }
                                            // store the change in PHPCR
                                            $referencingNode->getProperty($referencingField['property'])->addValue($uuid); // property should be correct type already
                                        }
                                    } else {
                                        // store the change in PHPCR
                                        $referencingNode->setProperty($referencingField['property'], [$uuid], $strategy);
                                    }

                                    // avoid confusion later, this change to the reference collection is already saved
                                    $collection->setDirty(false);
                                    break;
                                default:
                                    // in class metadata we only did a santiy check but not look at the actual mapping
                                    throw new MappingException(sprintf('Field "%s" of document "%s" is not a reference field. Error in referrer annotation: '.self::objToStr($document, $this->dm), $mapping['referencedBy'], ClassUtils::getClass($fv)));
                            }
                        }
                    }
                } elseif ('child' === $mapping['type']) {
                    if ($fieldValue === null && $node->hasNode($mapping['nodeName'])) {
                        $child = $node->getNode($mapping['nodeName']);
                        $childDocument = $this->getOrCreateDocument(null, $child);
                        $this->purgeChildren($childDocument);
                        $child->remove();
                    }
                }
            }

            if (!empty($this->documentChangesets[$oid]['reorderings'])) {
                foreach ($this->documentChangesets[$oid]['reorderings'] as $reorderings) {
                    foreach ($reorderings as $srcChildRelPath => $destChildRelPath) {
                        $node->orderBefore($srcChildRelPath, $destChildRelPath);
                    }
                }
            }

            $this->doSaveTranslation($document, $node, $class);

            if ($dispatchEvents) {
                if ($invoke = $this->eventListenersInvoker->getSubscribedSystems($class, Event::postUpdate)) {
                    $this->eventListenersInvoker->invoke(
                        $class,
                        Event::postUpdate,
                        $document,
                        new LifecycleEventArgs($document, $this->dm),
                        $invoke
                    );
                }
            }
        }
    }
442498
private function executeUpdates(ClassMetadata $class, array $options = array())
    {
        $className = $class->name;
        $persister = $this->getXmlEntityPersister($className);

        $hasPreUpdateLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::preUpdate]);
        $hasPreUpdateListeners = $this->evm->hasListeners(Events::preUpdate);
        $hasPostUpdateLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postUpdate]);
        $hasPostUpdateListeners = $this->evm->hasListeners(Events::postUpdate);

        foreach ($this->entityUpdates as $oid => $xmlEntity) {
            if (get_class($xmlEntity) == $className || $xmlEntity  instanceof Proxy && $xmlEntity instanceof $className) {
//                if ( ! $class->isEmbeddedDocument) {
                if ($hasPreUpdateLifecycleCallbacks) {
                    $class->invokeLifecycleCallbacks(Events::preUpdate, $xmlEntity);
    //                        $this->recomputeSingleDocumentChangeSet($class, $xmlEntity);
                }

                if ($hasPreUpdateListeners) {
                    $this->evm->dispatchEvent(Events::preUpdate, new Event\PreUpdateEventArgs(
                        $xmlEntity, $this->xem, $this->entityChangeSets[$oid])
                    );
                }

                $persister->update($xmlEntity);
                unset($this->entityUpdates[$oid]);

                if ($hasPostUpdateLifecycleCallbacks) {
                    $class->invokeLifecycleCallbacks(Events::postUpdate, $xmlEntity);
                }
                if ($hasPostUpdateListeners) {
                    $this->evm->dispatchEvent(Events::postUpdate, new Event\LifecycleEventArgs($xmlEntity, $this->xem));
                }
//                    $this->cascadePostUpdateAndPostPersist($class, $xmlEntity);
            }
        }
    }
1085732
private function executeInserts($class)
    {
        $entities   = array();
        $className  = $class->name;
        $persister  = $this->getEntityPersister($className);
        $invoke     = $this->listenersInvoker->getSubscribedSystems($class, Events::postPersist);

        foreach ($this->entityInsertions as $oid => $entity) {

            if ($this->em->getClassMetadata(get_class($entity))->name !== $className) {
                continue;
            }

            $persister->addInsert($entity);

            unset($this->entityInsertions[$oid]);

            if ($invoke !== ListenersInvoker::INVOKE_NONE) {
                $entities[] = $entity;
            }
        }

        $postInsertIds = $persister->executeInserts();

        if ($postInsertIds) {
            // Persister returned post-insert IDs
            foreach ($postInsertIds as $postInsertId) {
                $id      = $postInsertId['generatedId'];
                $entity  = $postInsertId['entity'];
                $oid     = spl_object_hash($entity);
                $idField = $class->identifier[0];

                $class->reflFields[$idField]->setValue($entity, $id);

                $this->entityIdentifiers[$oid] = array($idField => $id);
                $this->entityStates[$oid] = self::STATE_MANAGED;
                $this->originalEntityData[$oid][$idField] = $id;

                $this->addToIdentityMap($entity);
            }
        }

        foreach ($entities as $entity) {
            $this->listenersInvoker->invoke($class, Events::postPersist, $entity, new LifecycleEventArgs($entity, $this->em), $invoke);
        }
    }
760938
public function pushResult($class)
    {
        if (!$this->hasResult($class)) {
            $this->map[$class] = empty($this->map) ? 1
                : max(self::getResults()) + 1;
        }
    }
1315666
public function add($documents)
    {
        if (!is_array($documents)) {
            $documents = array($documents);
        }

        $add =& Archive::getByRef($this, 'add', array());
        foreach ($documents as $document) {
            $add[] = $document;
        }
    }
1182519
public function indexMultiple(array $documents)
    {
        $index = $this->client->getIndex($this->indexName);

        foreach ($documents as $document) {
            $this->indexDocument($document, $index);
        }

        $index->refresh();
    }
418267
public function mapManyEmbedded(ClassMetadata $class, $type = Type::LIST_)
    {
        $this->embedded = true;
        $this->type     = $type;

        $this->mapAttribute($class);
    }
1206408
public function upsert($document, $parentPath = null)
    {
        $class     = $this->dm->getClassMetadata(get_class($document));
        $changeset = $this->uow->getDocumentChangeSet($document);

        if (!$class->isEmbeddedDocument) {
            $qb         = $this->dm->createQueryBuilder($class->name);
            $identifier = $class->createIdentifier($document);

            $qb->update();
            $qb->setIdentifier($identifier);
        } elseif ($parentPath === null) {
            throw new \InvalidArgumentException;
        }

        foreach ($changeset as $attributeName => $change) {
            if ($class->isIdentifier($attributeName)) {
                continue;
            }

            list($old, $new) = $change;

            $mapping            = $class->getPropertyMetadata($attributeName);
            $discriminatorValue = null;

            if ($mapping->embedded && $mapping->isOne()) {
                $embeddedClassName  = get_class($new);
                $embeddedClass      = $this->dm->getClassMetadata($embeddedClassName);
                $discriminatorValue = $embeddedClass->getEmbeddedDiscriminatorValue($mapping, $new);
                //@Many will set discriminator in updatePath
            }

            if ($class->isEmbeddedDocument) {
                $path = $parentPath->map($attributeName, $discriminatorValue);
            } else {
                $path = $qb->attr($attributeName, $discriminatorValue);
            }

            $this->updatePath($path, $old, $new, true);
        }

        // add discriminator if the class has one
        if ($class->discriminatorAttribute !== null) {
            if ($class->isEmbeddedDocument) {
                $path = $parentPath->map($class->discriminatorAttribute);
            } else {
                $path = $qb->attr($class->discriminatorAttribute);
            }

            $path->set(
                $class->discriminatorValue !== null
                    ? $class->discriminatorValue
                    : $class->name
            );
        }

        if ($this->class->isVersioned) {
            // Set the initial version. Only supported on top-level documents.

            $versionMapping = $this->class->getPropertyMetadata($this->class->versionAttribute);
            $nextVersion    = null;

            if ($versionMapping->type === Type::INT) {
                $nextVersion = max(1, (int)$versionMapping->getValue($document));
            } elseif ($versionMapping->isDateType()) {
                $nextVersion = new \DateTime();
            }

            $nextVersion = $versionMapping->getType()->convertToDatabaseValue($nextVersion);
            $versionMapping->setValue($document, $nextVersion);

            $qb->attr($versionMapping->name)->set($nextVersion);
        }

        //DynamoDB will upsert even if we only pass the id conditional with no UpdateExpression

        if (!$class->isEmbeddedDocument) {
            $qb->getQuery()->execute();
        }
    }
367333
public function updateContentClassNames(&$classes)
    {
        if (!$this->apply()) {
            return;
        }
        
        foreach ($this->getTextAlignmentClassNames() as $class) {
            $classes[] = $class;
        }
        
        foreach ($this->getImageAlignmentClassNames() as $class) {
            $classes[] = $class;
        }
    }
224362
private function executeUpdates(ClassMetadata $class, array $options = array())
    {
        $className = $class->name;
        $persister = $this->getDocumentPersister($className);

        $hasPreUpdateLifecycleCallbacks = ! empty($class->lifecycleCallbacks[Events::preUpdate]);
        $hasPreUpdateListeners = $this->evm->hasListeners(Events::preUpdate);
        $hasPostUpdateLifecycleCallbacks = ! empty($class->lifecycleCallbacks[Events::postUpdate]);
        $hasPostUpdateListeners = $this->evm->hasListeners(Events::postUpdate);

        foreach ($this->documentUpdates as $oid => $document) {
            if (get_class($document) == $className || $document instanceof Proxy && $document instanceof $className) {
                if ( ! $class->isEmbeddedDocument) {
                    if ($hasPreUpdateLifecycleCallbacks) {
                        $class->invokeLifecycleCallbacks(Events::preUpdate, $document);
                        $this->recomputeSingleDocumentChangeSet($class, $document);
                    }

                    if ($hasPreUpdateListeners && isset($this->documentChangeSets[$oid])) {
                        $this->evm->dispatchEvent(Events::preUpdate, new Event\PreUpdateEventArgs(
                            $document, $this->dm, $this->documentChangeSets[$oid])
                        );
                    }
                    $this->cascadePreUpdate($class, $document);
                }

                if ( ! $class->isEmbeddedDocument && isset($this->documentChangeSets[$oid]) && $this->documentChangeSets[$oid]) {
                    $persister->update($document, $options);
                }
                unset($this->documentUpdates[$oid]);

                if ( ! $class->isEmbeddedDocument) {
                    if ($hasPostUpdateLifecycleCallbacks) {
                        $class->invokeLifecycleCallbacks(Events::postUpdate, $document);
                    }
                    if ($hasPostUpdateListeners) {
                        $this->evm->dispatchEvent(Events::postUpdate, new LifecycleEventArgs($document, $this->dm));
                    }
                    $this->cascadePostUpdateAndPostPersist($class, $document);
                }
            }
        }
    }
1208567
private function executeMoves($documents)
    {
        foreach ($documents as $oid => $value) {
            if (!$this->contains($oid)) {
                continue;
            }

            list($document, $targetPath) = $value;

            $sourcePath = $this->getDocumentId($document);
            if ($sourcePath === $targetPath) {
                continue;
            }

            $class = $this->dm->getClassMetadata(get_class($document));
            if ($invoke = $this->eventListenersInvoker->getSubscribedSystems($class, Event::preMove)) {
                $this->eventListenersInvoker->invoke(
                    $class,
                    Event::preMove,
                    $document,
                    new MoveEventArgs($document, $this->dm, $sourcePath, $targetPath),
                    $invoke
                );
            }

            $parentNode = $this->session->getNode(PathHelper::getParentPath($targetPath));
            $this->validateChildClass($parentNode, $class);

            $this->session->move($sourcePath, $targetPath);

            // update fields nodename, parentMapping and depth if they exist in this type
            $node = $this->session->getNode($targetPath); // get node from session, document class might not map it
            if ($class->nodename) {
                $class->setFieldValue($document, $class->nodename, $node->getName());
            }

            if ($class->parentMapping) {
                $class->setFieldValue($document, $class->parentMapping, $this->getOrCreateProxyFromNode($node->getParent(), $this->getCurrentLocale($document, $class)));
            }

            if ($class->depthMapping) {
                $class->setFieldValue($document, $class->depthMapping, $node->getDepth());
            }

            // update all cached children of the document to reflect the move (path id changes)
            foreach ($this->documentIds as $childOid => $id) {
                if (0 !== strpos($id, $sourcePath)) {
                    continue;
                }

                $newId = $targetPath.substr($id, strlen($sourcePath));
                $this->documentIds[$childOid] = $newId;

                $child = $this->getDocumentById($id);
                if (!$child) {
                    continue;
                }

                unset($this->identityMap[$id]);
                $this->identityMap[$newId] = $child;

                $childClass = $this->dm->getClassMetadata(get_class($child));
                if ($childClass->identifier) {
                    $childClass->setIdentifierValue($child, $newId);
                    if (!$child instanceof Proxy || $child->__isInitialized()) {
                        $this->originalData[$childOid][$childClass->identifier] = $newId;
                    }
                }
            }

            if ($invoke = $this->eventListenersInvoker->getSubscribedSystems($class, Event::postMove)) {
                $this->eventListenersInvoker->invoke(
                    $class,
                    Event::postMove,
                    $document,
                    new MoveEventArgs($document, $this->dm, $sourcePath, $targetPath),
                    $invoke
                );
            }
        }
    }
567692
protected void updateClasses(HashMap<String,List<Class>> list, String superclass, List<Class> classes) {
    if (!list.containsKey(superclass)) {
      list.put(superclass, classes);
    }
    else {
      for (Class cls : classes) {
        if (!list.get(superclass).contains(cls))
          list.get(superclass).add(cls);
      }
    }
  }
247423
public function setClasses($classes, $override = true)
    {
        foreach ($classes as $key => $class) {
            if ($override || !isset($this->_classes[$key])) {
                $this->_classes[$key] = $class;
            }
        }
    }
1307002
private function executeInserts($class, array $options = array())
    {
        $className = $class->name;
        $persister = $this->getXmlEntityPersister($className);

        $hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postPersist]);
        $hasListeners = $this->evm->hasListeners(Events::postPersist);
        if ($hasLifecycleCallbacks || $hasListeners) {
            $documents = array();
        }

        $inserts = array();
        foreach ($this->entityInsertions as $oid => $xmlEntity) {
            if (get_class($xmlEntity) === $className) {
                $persister->insert($xmlEntity);
                unset($this->entityInsertions[$oid]);
            }
        }
    }