neo4j-php-ogm 라이브러리를 사용하여 2 개의 관계를 통해 속성에 액세스하는 방법에 대해 머리 글자가 없습니다.NEO4J-PHP-OGM과 2 관계를 통해 노드의 속성에 액세스하는 방법
예를 들어, MANY "resource"노드에 연결되는 "사용자"노드가 있습니다.이 노드는 각각 미리 정의 된 "MetaResource"노드의 고정 된 수에 연결됩니다. "자원"노드는 등록 정보를 가지며 "MetaResource"노드는 각 자원 유형의 메타 등록 정보를 갖습니다. "사용자"노드에서 시작하여 "MetaResource"노드의 속성에 액세스하는 방법을 어떻게 알 수 있습니까?
(user)-[r:HAS_RESOURCE]->(resource)-[m:METARESOURCE]->(metaResource)
내 예를 들어 사용자 개체 :
/**
* @OGM\Node(label="User")
*/
class User
{
/**
* @OGM\GraphId()
* @var int
*/
private $id;
/**
* @OGM\Relationship(type="HAS_RESOURCE", direction="OUTGOING", targetEntity="\AppBundle\Entity\Resources", collection=true)
* @var ArrayCollection|\AppBundle\Entity\Resources[]
*/
protected $resources;
/**
* @return \Doctrine\Common\Collections\ArrayCollection|\AppBundle\Entity\Resources[]
*/
public function getResources()
{
return $this->resources;
}
/**
* @return \Doctrine\Common\Collections\ArrayCollection|\AppBundle\Entity\Resources[]
*/
public function getResource($name)
{
foreach($this->resources as $resource){
if($resource->getResourceType() == $name){
return $resource;
break;
}
}
}
/**
* @param AppBundle\Entity\Resources $resources
*/
public function addResource(Resources $resources)
{
if (!$this->resources->contains($resources)) {
$this->resources->add($resources);
}
}
}
내 예 자원 엔티티 :
/**
* @OGM\Node(label="Resources")
*/
class Resources
{
/**
* @OGM\GraphId()
* @var int
*/
protected $id;
/**
* @OGM\Property(type="int")
* @var int
*/
protected $resourcecount;
/**
* @OGM\Relationship(type="METARESOURCE", direction="OUTGOING", targetEntity="\AppBundle\Entity\MetaResource", collection=false)
* @var MetaResource
*/
protected $metaResource;
/**
* @param \AppBundle\Entity\MetaResource $metaResource
*/
public function __construct(MetaResource $metaResource)
{
$this->metaResource = $metaResource;
}
public function getId()
{
return $this->id;
}
public function getResourceCount()
{
return $this->resourcecount;
}
public function setResourceCount($resourcecount)
{
$this->resourcecount = $resourcecount;
}
/**
* @return \AppBundle\Entity\MetaResource
*/
public function getMetaResource()
{
return $this->metaResource;
}
}
그리고 내 예 MetaResource 엔티티 :
/**
* @OGM\Node(label="MetaResource")
*/
class MetaResource
{
/**
* @OGM\GraphId()
* @var int
*/
protected $id;
/**
* @OGM\Property(type="string")
* @var string
*/
protected $resourceType;
/**
* @OGM\Property(type="string")
* @var string
*/
protected $name_DE;
/**
* @OGM\Property(type="string")
* @var string
*/
protected $icon;
/**
* @OGM\Property(type="string")
* @var string
*/
protected $iconColour;
/**
* @OGM\Property(type="string")
* @var string
*/
protected $colour;
public function getResourceType()
{
return $this->resourceType;
}
public function getName_DE()
{
return $this->name_DE;
}
public function getIcon()
{
return $this->icon;
}
public function getIconColour()
{
return $this->iconColour;
}
public function getColour()
{
return $this->colour;
}
}
Neo4j에서 하나 개의 경로는 다음과 같습니다
마지막으로 관계를 설정 내 컨트롤러의 코드는 :
$metaResource=$em->getRepository(MetaResource::class)->findOneBy('resourceType', 'wood');
$rWood = new Resources($metaResource);
$rWood->setResourceCount(20);
$em->persist($rWood);
$em->flush();
$user->addResource($rWood);
내가 지금 Neo4j의 웹 콘솔을 살펴있는 경우, 모든 관계와 노드가 올바르게 삽입됩니다.
$user->getResources()
을 가진 사용자의 리소스를 얻었지만 모든 리소스 객체를 성공적으로 가져 왔지만 "$ metaResource"속성은 예상되는 MetaResource 객체의 객체 대신 항상 NULL입니다. 예를 들면 다음과 같습니다.
foreach($user->getResources() as $resource){
var_dump($resource->getMetaResource());
}
그런 다음 NULL 만 출력합니다.
반면에 직접 리소스 개체 (예 : $resource = $em->getRepository(Resources::class)->findOneById(123)
)를 가져온 다음 $resource->getMetaResource()
으로 연결된 MetaResource를 가져 오려고하면 작동합니다. 내가 뭘 놓치고 있니?
건배
안녕하세요 @ 요란 일반적으로 최신 베타 버전과 게으른 주석으로 처리해야하는지 확인하겠습니다. –
안녕하세요 Chris :) 관계 엔터티를 사용하고 속성을 저장하여 특정 사례를 해결했습니다. 관계. 따라서 (사용자) - [r : HAS_RESOURCE] -> (자원 {property1, property2}) - [m : METARESOURCE] -> (metaresource {property1, property2} - [r : HAS_RESOURCE {property1, property2}] -> (metaresource {property1, property2})'. 이 특정 사용 사례에 대해 잘 작동합니다. 그러나 여러 관계를 통해 데이터를 얻는 방법을 아는 것이 좋습니다! Im 나의 경우, 어떻게 그리고 어디에 게으른 로딩을 정의해야합니까? 건배 – Joran
문서 끝 부분에 작은 섹션을 추가했지만 일반적으로 사용 사례가 작동해야하며 버그가있을 수 있습니다. 예를 들어 테스트 사례로 사용하고 회신을 보내면 –