/** * Find the nodes related to the relationship * * @param Property $property * @param array $info * * @return object */ protected function getRelationshipNode(Property $property, array $info) { $nodeClass = $this->map->getClass($property->getOption('node')); foreach ($this->entities as $entity) { if (!$entity instanceof $nodeClass) { continue; } $nodeInfo = $this->entities->getInfo($entity); if ($nodeInfo['realId'] === $info[$property->getType()]) { return $entity; } } $query = new Query(sprintf('MATCH (n:%s) WHERE id(n) = {where}.id RETURN n;', $nodeClass)); $query->addVariable('n', $nodeClass); $query->addParameters('where', ['id' => $info[$property->getType()]]); return $this->uow->execute($query)->current(); }
/** * Find an entity by its id * * @param string $class * @param mixed $id * * @throws InvalidArgumentException If the class name is not recognized * @throws EntityNotFoundException If the entity is not found * * @return object */ public function find($class, $id) { if (!$this->identityMap->has($class)) { throw new \InvalidArgumentException(sprintf('The entity "%s" is not handled by this manager', $class)); } $class = $this->identityMap->getClass($class); if ($this->entitySilo->has($class, $id)) { return $this->entitySilo->get($class, $id); } $metadata = $this->metadataRegistry->getMetadata($class); if ($metadata instanceof NodeMetadata) { $format = sprintf('(e:%s)', $class); } else { $format = sprintf('()-[e:%s]-()', $class); } $query = new Query(sprintf('MATCH %s WHERE e.%s = {props}.id RETURN e;', $format, $metadata->getId()->getProperty())); $query->addVariable('e', $class)->addParameters('props', ['id' => $id], ['id' => sprintf('e.%s', $metadata->getId()->getProperty())]); $results = $this->execute($query); if ($results->count() === 0) { throw new EntityNotFoundException(sprintf('The entity "%s" with the id "%s" not found', $class, $id)); } return $results->current(); }
/** * Extract variables and parameters from the expression to inject them into the query * * @param Query $query * @param ExpressionInterface $expression * * @return void */ protected function extractData(Query $query, ExpressionInterface $expression) { if ($expression instanceof ParametrableExpressionInterface && $expression->hasParameters()) { $query->addParameters($expression->getParametersKey(), $expression->getParameters(), $expression->getReferences()); } if ($expression instanceof VariableAwareInterface && $expression->hasVariable() && $expression->hasAlias()) { $query->addVariable($expression->getVariable(), $expression->getAlias()); } }