container = $container; $this->neo = $row; $this->row = $row->getProperties(); } function reload(){ $cl = get_class($this); $o = new $cl($this->container, $this->neo); return $o; } function getNode(){ return $this->neo; } public function getId(){ $id = $this->neo->getId(); return $this->neo->getId(); } public function has($col){ return isset($this->row[$col]); } public function &__get($name){ //Call method $gname = 'get'.ucfirst($name); if (method_exists($this, $gname)){ $r=$this->$gname(); return $r; } if (!isset($this->row[$name])) $this->container->log("Vertex #".$this->getId()."'s property '$name' is not defined",'ERROR'); return $this->row[$name]; } public function &__call($name, $args){ //Call method $gname = 'get'.ucfirst($name); if (method_exists($this, $gname)){ $r=call_user_func_array(array($this,$gname), $args); return $r; } $this->container->log("Row method $name doesn't exist",'FATAL'); throw new \Exception(get_class($this).": Row method $name doesn't exist"); } public function __toString(){ return json_encode(array_merge(array("id" => $this->id), $this->row)); } public function relation_one($name, $rowClass, $dir = 'out'){ $direction = ($dir == 'out') ? \Everyman\Neo4j\Relationship::DirectionOut : \Everyman\Neo4j\Relationship::DirectionIn; $rs = $this->neo->getRelationships(array($name), $direction); if (sizeof($rs)==0) return false; $node = ($dir == 'out') ? $rs[0]->getEndNode() : $rs[0]->getStartNode(); return new $rowClass($this->container, $node); } //Get relationships // = '\FW\Database\Neo4jVertex' public function relation($name = null, $rowClass = '\FW\Database\Neo4jVertex', $dir = 'out', $cnt = 0){ if ($dir == 'out') $direction = \Everyman\Neo4j\Relationship::DirectionOut; elseif ($dir == 'in') $direction = \Everyman\Neo4j\Relationship::DirectionIn; $rs = $this->neo->getRelationships(array($name), $direction); $rels = array(); foreach ($rs as $r){ $node = ($dir == 'out') ? $r->getEndNode() : $r->getStartNode(); $rels[]=new $rowClass($this->container, $node); $cnt--; if ($cnt==0) return $rels; } return $rels; } public function add_relation($row, $relation_name){ if (get_class($row)=='FW\Database\Neo4jVertex'){ throw new \Exception('neo4jvertex->add_relation on Neo4jVertex object. Relation: '.$relation_name.' object->to_s: '.((string)$row)); } if ($this->in_relation($row, $relation_name)) return false; $relation = $this->neo->relateTo($row->getNode(), $relation_name)->setProperty('created_at', date("Y-m-d H:i:s")); $relation->save(); return $relation; } public function in_relation($row, $name = ''){ $path = $this->neo->findPathsTo($row->neo, $name, \Everyman\Neo4j\Relationship::DirectionOut)->getSinglePath(); if (count($path)!=2) return false; return true; } public function count_relations($type, $dir = 'out'){ if ($dir == 'out') $direction = \Everyman\Neo4j\Relationship::DirectionOut; elseif ($dir == 'in') $direction = \Everyman\Neo4j\Relationship::DirectionIn; $rs = $this->neo->getRelationships($type, $direction); return sizeof($rs); } public function remove_relation($row, $name = ''){ $path = $this->neo->findPathsTo($row->neo, $name, \Everyman\Neo4j\Relationship::DirectionOut)->getSinglePath(); if (count($path)!=2) return false; $rs = $path->getRelationships(); $rs[0]->delete(); return true; } public function remove_relations($name = '', $dir = DIRECTION_ALL){ if ($dir == DIRECTION_ALL) $direction = \Everyman\Neo4j\Relationship::DirectionAll; elseif ($dir == DIRECTION_OUT) $direction = \Everyman\Neo4j\Relationship::DirectionOut; elseif ($dir == DIRECTION_IN) $direction = \Everyman\Neo4j\Relationship::DirectionIn; if ($name == '') $rs = $this->neo->getRelationships(); else $rs = $this->neo->getRelationships(array($name), $direction); foreach ($rs as $r) $r->delete(); } public function update($array){ foreach ($array as $key => $value){ $this->neo->setProperty($key, $value); $this->row[$key] = $value; } $this->neo->save(); return $this; } public function delete(){ $this->neo->delete(); } } ?>