使用Neo4J构建关系实体PHP OGM EntityManager

时间:2017-10-03 09:56:07

标签: php neo4j neo4j-php-ogm

我正在尝试使用这个简单的方法为GraphAware Neo4j PHP OGM library在Neo4j数据库中为我的关系构建一个实体对象:

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

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

这里我们有实体类,关系优先:

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;

}

然后,启动节点:

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;
}

..和结束节点:

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);
    }
}

显然,对于节点entites来说效果非常好,会触发以下关系错误:

  

致命错误:在/vendor/graphaware/neo4j-php-ogm/src/EntityManager.php中调用未定义的方法GraphAware \ Neo4j \ OGM \ Metadata \ RelationshipEntityMetadata :: hasCustomRepository()

有什么建议我可以解决这个问题吗?我甚至尝试使用以下方式EntityManager::createQuery()

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();
}

但是,显然,addEntityMapping()对关系实体也不起作用! (它可能是一个功能,而不是一个错误):

  

捕获致命错误:参数1传递给GraphAware \ Neo4j \ OGM \ Hydrator \ EntityHydrator :: hydrateNode()必须实现接口GraphAware \ Common \ Type \ Node,GraphAware \ Bolt \ Result \ Type \ Relationship的实例,调用在第145行的/vendor/graphaware/neo4j-php-ogm/src/Query.php中,在第232行的/vendor/graphaware/neo4j-php-ogm/src/Hydrator/EntityHydrator.php中定义

所以,我最终得知我可以使用这个库在Neo4J中轻松定义和存储关系实体,但不知道如何使用EntityManager轻松检索它,就像我可以用节点一样。

非常感谢任何帮助!

根据以下评论中的要求,这些是我正在使用的GraphAware软件包:

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 个答案:

没有答案