/** * Create all queries to insert al new entities * * @return Query */ protected function computeInsertQuery() { $nodes = new \SplObjectStorage(); $rels = new \SplObjectStorage(); $qb = new QueryBuilder(); $nodeIndex = 0; $relIndex = 0; foreach ($this->scheduledForInsert as $entity) { $class = $this->getClass($entity); $metadata = $this->metadataRegistry->getMetadata($class); if ($metadata instanceof NodeMetadata) { $nodes->attach($entity, $nodeIndex); $nodeIndex++; } else { $rels->attach($entity, $relIndex); $relIndex++; } } $matchNodeIdx = 0; $relsToCreate = []; foreach ($rels as $rel) { $class = $this->getClass($rel); $metadata = $this->metadataRegistry->getMetadata($class); $startNode = $this->accessor->getValue($rel, $metadata->getStartNode()); $endNode = $this->accessor->getValue($rel, $metadata->getEndNode()); if (!$nodes->contains($startNode)) { $startNodeClass = $this->getClass($startNode); $startNodeMeta = $this->metadataRegistry->getMetadata($startNodeClass); $startNodeIdProp = $startNodeMeta->getId()->getProperty(); $startNodeId = $this->accessor->getValue($startNode, $startNodeIdProp); $startVar = 'mn' . (string) $matchNodeIdx; $matchNodeIdx++; $qb->matchNode($startVar, $startNodeClass, [$startNodeIdProp => $startNodeId]); } else { $startVar = 'n' . (string) $nodes[$startNode]; } if (!$nodes->contains($endNode)) { $endNodeClass = $this->getClass($endNode); $endNodeMeta = $this->metadataRegistry->getMetadata($endNodeClass); $endNodeIdProp = $endNodeMeta->getId()->getProperty(); $endNodeId = $this->accessor->getValue($endNode, $endNodeIdProp); $endVar = 'mn' . (string) $matchNodeIdx; $matchNodeIdx++; $qb->matchNode($endVar, $endNodeClass, [$endNodeIdProp => $endNodeId]); } else { $endVar = 'n' . (string) $nodes[$endNode]; } $data = $this->getEntityData($rel, $metadata); $relsToCreate[] = ['startVar' => $startVar, 'endVar' => $endVar, 'var' => 'r' . (string) $rels[$rel], 'class' => $metadata->getClass(), 'data' => $data]; } foreach ($nodes as $node) { $class = $this->getClass($node); $metadata = $this->metadataRegistry->getMetadata($class); $data = $this->getEntityData($node, $metadata); $qb->create('n' . (string) $nodes[$node], $class, $data); } foreach ($relsToCreate as $rel) { $qb->createRelationship($rel['startVar'], $rel['endVar'], $rel['var'], $rel['class'], $rel['data']); } return $qb->getQuery(); }