public function __construct(EntityManager $em, $baseEntity, $targetEntityClass, RelationshipMetadata $relationshipMetadata, $initialEntity = null) { $this->finder = $relationshipMetadata->isRelationshipEntity() ? new RelationshipEntityFinder($em, $targetEntityClass, $relationshipMetadata, $baseEntity) : new RelationshipsFinder($em, $targetEntityClass, $relationshipMetadata); $this->em = $em; $this->collection = new Collection(); $this->baseId = $this->em->getClassMetadataFor(get_class($baseEntity))->getIdValue($baseEntity); $this->initialEntity = $initialEntity; $this->baseEntityClass = get_class($baseEntity); $this->relationshipMetadata = $relationshipMetadata; $this->baseInstance = $baseEntity; }
public function getDeleteRelationshipQuery($entityIdA, $entityIdB, RelationshipMetadata $relationship) { $relString = ''; switch ($relationship->getDirection()) { case 'OUTGOING': $relString = '-[r:%s]->'; break; case 'INCOMING': $relString = '<-[r:%s]-'; break; case 'BOTH': $relString = '-[r:%s]-'; break; default: throw new \InvalidArgumentException(sprintf('Direction "%s" is not valid', $relationship->getDirection())); } $relStringPart = sprintf($relString, $relationship->getType()); $query = 'MATCH (a), (b) WHERE id(a) = {ida} AND id(b) = {idb} MATCH (a)' . $relStringPart . '(b) DELETE r'; return Statement::create($query, ['ida' => $entityIdA, 'idb' => $entityIdB]); }
public function hydrateRelationshipEntity(RelationshipEntityMetadata $reMetadata, array $reMap, NodeEntityMetadata $startNodeMetadata, NodeEntityMetadata $endNodeMetadata, $baseInstance, RelationshipMetadata $relationshipEntity, $pov = null) { /** @var \GraphAware\Neo4j\Client\Formatter\Type\Relationship $rel */ $rel = $reMap['rel']; $relId = $rel->identity(); if (null !== ($possibleRE = $this->entityManager->getUnitOfWork()->getRelationshipEntityById($relId))) { return $possibleRE; } $start = $this->hydrateNode($reMap['start'], $startNodeMetadata->getClassName(), true); $end = $this->hydrateNode($reMap['end'], $endNodeMetadata->getClassName(), true); $reInstance = $reMetadata->newInstance(); $reMetadata->setId($reInstance, $relId); $reMetadata->setStartNodeProperty($reInstance, $start); $reMetadata->setEndNodeProperty($reInstance, $end); $this->entityManager->getUnitOfWork()->addManagedRelationshipEntity($reInstance, $baseInstance, $relationshipEntity->getPropertyName()); $reMetadata->setId($reInstance, $relId); $otherToSet = $relationshipEntity->getDirection() === 'INCOMING' ? $reMetadata->getStartNodeValue($reInstance) : $reMetadata->getEndNodeValue($reInstance); $possiblyMapped = $relationshipEntity->getDirection() === 'INCOMING' ? $reMetadata->getStartNodePropertyName() : $reMetadata->getEndNodePropertyName(); $otherMetadata = $this->entityManager->getClassMetadataFor(get_class($otherToSet)); foreach ($otherMetadata->getRelationships() as $relationship) { if ($relationship->getDirection() !== $relationshipEntity->getDirection() && $relationship->hasMappedByProperty() && $relationship->getMappedByProperty() === $possiblyMapped) { if ($relationship->isCollection()) { if (null !== $this->entityManager->getUnitOfWork()->getEntityById($otherMetadata->getIdValue($otherToSet))) { $relationship->initializeCollection($otherToSet); $relationship->addToCollection($otherToSet, $reInstance); } } else { if (null === $relationship->getValue($otherToSet)) { $relationship->setValue($otherToSet, $reInstance); } } } } foreach ($rel->values() as $k => $value) { if (null !== ($prop = $reMetadata->getPropertyMetadata($k))) { $prop->setValue($reInstance, $value); } } //$this->entityManager->getUnitOfWork()->addManagedRelationshipEntity($reInstance, $baseInstance, $relationshipEntity->getPropertyName()); return $reInstance; }
public function scheduleRelationshipReferenceForCreate($entity, $target, RelationshipMetadata $relationship) { $this->relationshipsScheduledForCreated[] = [$entity, $relationship, $target, $relationship->getPropertyName()]; }