Building relationship entity with Neo4J PHP OGM EntityManager

429 Views Asked by At

I am trying to build an entity object for my relationship in Neo4j database with GraphAware Neo4j PHP OGM library using this simple method:

public function getRelationshipEntity($entityId) {
    $repo = $this->entityManager->getRepository( Entity\Relationship\Fired::class );

    return $repo->findOneById($entityId);
}

Here we have the entity classes, relationship first:

namespace Entity\Relationship;

use GraphAware\Neo4j\OGM\Annotations as OGM;
use Entity\Issue;
use Entity\Event;

/**
 * @OGM\RelationshipEntity(type="FIRED")
 */
class Fired {
    /**
     * @OGM\GraphId()
     */
    protected $id;

    /**
     * @OGM\StartNode(targetEntity="Entity\Event")
     */
    protected $event;

    /**
     * @OGM\EndNode(targetEntity="Entity\Issue")
     */
    protected $issue;

    /**
     * @var string
     *
     * @OGM\Property(type="string")
     */
    protected $time;

    /**
     * @var string
     *
     * @OGM\Property(type="string")
     */
    protected $eventName;

}

Then, start node:

namespace Entity;

use GraphAware\Neo4j\OGM\Annotations as OGM;

/**
 * @OGM\Node(label="Event")
 */
class Event {

    /**
     * @OGM\GraphId()
     */
    protected $id;

    /**
     * @var string
     *
     * @OGM\Property(type="string")
     */
    protected $name;
}

..and end node:

namespace Entity;

use Doctrine\Common\Collections\ArrayCollection;
use GraphAware\Neo4j\OGM\Annotations as OGM;

/**
 * @OGM\Node(label="Issue")
 */
class Issue {

    /**
     * @OGM\GraphId()
     */
    protected $id;

    /**
     * @OGM\Property(type="string")
     */
    protected $key;

    /**
     * @OGM\Property(type="string")
     */
    protected $created;

    /**
     * @OGM\Property(type="string")
     */
    protected $updated;

    /**
     * @OGM\Relationship(type="FIRED", direction="INCOMING", relationshipEntity="Entity\Relationship\Fired", collection=true)
     * @var ArrayCollection
     */
    protected $eventFires;

    public function __construct($key) {
        $this->key = $key;
        $this->eventFires = new ArrayCollection();
    }

    public function __wakeup() {
        $this->__construct($this->key);
    }

    /**
     * @return ArrayCollection
     */
    public function getEventFires() {
        return $this->eventFires;
    }

    public function addEventFire(Entity\Relationship\Fired $eventFired) {
        $this->eventFires->add($eventFired);
    }

    public function removeEventFire(Entity\Relationship\Fired $eventFired) {
        $this->eventFires->removeElement($eventFired);
    }
}

Apparently, what works really well for node entites, triggers the following error for relationships:

Fatal error: Call to undefined method GraphAware\Neo4j\OGM\Metadata\RelationshipEntityMetadata::hasCustomRepository() in /vendor/graphaware/neo4j-php-ogm/src/EntityManager.php

Any suggestion how I could workaround this? I even tried using EntityManager::createQuery() the following way:

public function getRelationships($eventName) {
    $query = $this->entityManager->createQuery('MATCH (e:Event)-[f:FIRED{eventName: {eventName}}]->() RETURN e,f ORDER BY f.time');

    $query->addEntityMapping('e', 'Entity\Event' );
    $query->addEntityMapping('f', 'Entity\Relationship\Fired' );

    $query->setParameter( 'eventName', $eventName);

    return $query->execute();
}

But, apparently, addEntityMapping() doesn't work for relationship entities either! (It might be a feature though, not a bug):

Catchable fatal error: Argument 1 passed to GraphAware\Neo4j\OGM\Hydrator\EntityHydrator::hydrateNode() must implement interface GraphAware\Common\Type\Node, instance of GraphAware\Bolt\Result\Type\Relationship given, called in /vendor/graphaware/neo4j-php-ogm/src/Query.php on line 145 and defined in /vendor/graphaware/neo4j-php-ogm/src/Hydrator/EntityHydrator.php on line 232

So, I ended up that I can easily define and store relationship entities in Neo4J with this library but not sure how I could retrieve it easily with EntityManager, in the similar way I can do so with nodes.

Any help would be much appreciated!

As requested in comment below, these are GraphAware packages that I am using:

graphaware/neo4j-bolt               1.9.1              Neo4j Bolt Binary Protocol PHP Driver
graphaware/neo4j-common             3.4.0              Common Utilities library for Neo4j
graphaware/neo4j-php-client         4.8.0              Neo4j-PHP-Client is the most advanced PHP Client for Neo4j
graphaware/neo4j-php-ogm            1.0.0-RC6          PHP Object Graph Mapper for Neo4j
0

There are 0 best solutions below