/** * Adds a relationship to the relationship collection * * @param Relationship $relationship * @return bool * @throws SchemaDefinitionException */ public function addRelationship(Relationship $relationship) { foreach ($this->relationships as $rel) { if ($rel->getType() === $relationship->getType() && $rel->getStartNode() === $relationship->getStartNode() && $rel->getEndNode() === $relationship->getEndNode()) { throw new SchemaDefinitionException(sprintf('There is already a relationship declared with TYPE "%s" and STARTNODE "%s" and ENDNODE "%s"', $relationship->getType(), $relationship->getStartNode(), $relationship->getEndNode())); } } return $this->relationships->add($relationship); }
function it_should_throw_error_when_creating_duplicated_relationships(Relationship $relationship) { $relationship->getType()->willReturn('RELATES'); $relationship->getStartNode()->willReturn('person'); $relationship->getEndNode()->willReturn('company'); $this->addRelationship($relationship); $this->shouldThrow('Neoxygen\\Neogen\\Exception\\SchemaDefinitionException')->duringAddRelationship($relationship); }
/** * @param RelationshipDefinition $relationship * @param null|int $seed */ public function process1ToNRelationship(RelationshipDefinition $relationship, $seed) { $startNodes = $this->nodesByIdentifier[$relationship->getStartNode()]; $endNodes = $this->nodesByIdentifier[$relationship->getEndNode()]; $target = $this->calculateApproxTargetNodes($startNodes->count(), $endNodes->count()); $maxIteration = 1 === $target ? $startNodes->count() : $startNodes->count() - 1; if ($endNodes->count() > $startNodes->count()) { if ($endNodes->count() % $startNodes->count() === 0) { $maxIteration = $startNodes->count(); } } $ec = $endNodes->count(); $eci = 0; $ssi = 0; for ($s = 0; $s < $maxIteration; $s++) { for ($i = 0; $i < $target; $i++) { $startNode = $startNodes->get($s); $endNode = $endNodes->get($eci); $this->createRelationship($relationship->getType(), $startNode->getId(), $startNode->getLabel(), $endNode->getId(), $endNode->getLabel(), $relationship->getProperties(), $seed); $eci++; } $ssi++; } if ($ssi < $startNodes->count()) { $lastStartNode = $startNodes->get($ssi); for ($eci; $eci < $ec; $eci++) { $endNode = $endNodes->get($eci); $this->createRelationship($relationship->getType(), $lastStartNode->getId(), $lastStartNode->getLabel(), $endNode->getId(), $endNode->getLabel(), $relationship->getProperties(), $seed); } } }