How to access properties of node over 2 relations with NEO4J-PHP-OGM

282 Views Asked by At

I cant get my head around how to access properties over 2 relationships with the neo4j-php-ogm library.

Say for example I have a "user" node, which connects to MANY "resource" nodes, which in return are each connected to a fixed number of predefined "MetaResource" node. The "resource" nodes have properties and the "MetaResource" nodes have the Meta-Properties of each resource type. How can I know access the properties of the "MetaResource" nodes starting from a "user" node? In Neo4j one such route looks like this:

(user)-[r:HAS_RESOURCE]->(resource)-[m:METARESOURCE]->(metaResource)

My example user entity:

/**
 * @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);
        }
    }
}

My Example Resource Entity:

/**
 * @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;
    }


}

And my Example MetaResource Entity:

/**
 * @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;
    }

}

And finally the code from my controller, which sets up the relations:

$metaResource=$em->getRepository(MetaResource::class)->findOneBy('resourceType', 'wood');  
$rWood = new Resources($metaResource);            
$rWood->setResourceCount(20);
$em->persist($rWood);
$em->flush();
$user->addResource($rWood);

If I now have a look at the Neo4j webconsole, all relationships and nodes are correctly inserted.

Now, if I get the resources of a user with $user->getResources() I successfully get all the resource objects, but the "$metaResource" property is always NULL instead of the anticipated Object of my MetaResource entity. For example if I do:

foreach($user->getResources() as $resource){
    var_dump($resource->getMetaResource());        
}

Then it outputs only NULLs.

On the other hand, if I directly get a resource object (for example with $resource = $em->getRepository(Resources::class)->findOneById(123) ) and then try to get the connected MetaResource with $resource->getMetaResource() it works. What am I missing?

Cheers

1

There are 1 best solutions below

1
On

I have made some progress regarding this use case. I'm using now a proxy generator that can handle this use case (this was a big missing part into the lib actually but takes time to implement).

So please test with the latest release 1.0.0-beta7.