/** * Get the direction of a relationship out of a Relation instance. * * @param \\Orientdb\Relationship $relation * @param \Sgpatil\Orientdb\Eloquent\Model $parent * @param \Sgpatil\Orientdb\Eloquent\Model $related * @return string Either 'in' or 'out' */ public function directionFromRelation(Relationship $relation, Model $parent, Model $related) { // We will match the ids of the parent model and the start node of the relationship // and if they match we know that the direction is outgoing, incoming otherwise. $node = $relation->getStartNode(); // We will start by considering the relationship direction to be 'incoming' until // we match and find otherwise. $direction = 'in'; if ($node->getId() === $parent->getKey()) { $direction = 'out'; } return $direction; }
/** * Set a given relationship on this relation. * * @param \\Orientdb\Relationship $relation */ public function setRelation(Relationship $relation, $debug = false) { // Set the relation object. $this->relation = $relation; // Replace the attributes with those brought from the given relation. $this->attributes = $relation->getProperties(); $this->setAttribute($this->primaryKey, $relation->getId()); // Set the start and end nodes. $this->start = $relation->getStartNode(); $this->end = $relation->getEndNode(); // Instantiate and fill out the related model. $relatedNode = $this->isDirectionOut() ? $this->end : $this->start; $attributes = array_merge(['id' => $relatedNode->getId()], $relatedNode->getProperties()); // This is an existing relationship. $exists = true; $this->related = $this->related->newFromBuilder($attributes, $exists); $this->related->setConnection($this->related->getConnection()); }