我正在尝试使用以下简单方法使用GraphAware Neo4j PHP OGM 库为我在 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);
}
}
显然,对节点实体非常有效的方法会触发以下关系错误:
致命错误:调用 /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()
也不适用于关系实体!(虽然它可能是一个功能,而不是一个错误):
可捕获的致命错误:传递给 GraphAware\Neo4j\OGM\Hydrator\EntityHydrator::hydraNode() 的参数 1 必须实现接口 GraphAware\Common\Type\Node,给出 GraphAware\Bolt\Result\Type\Relationship 的实例,在 /vendor 中调用/graphaware/neo4j-php-ogm/src/Query.php 在第 145 行,并在第 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