Find a Node by the id in neo4j

1

I am working with neo4j, I need to make a query to recover a node by the id and can not find the node that I specify, if I filter for another property it does find it but if I specify the id it does not find it. If I go directly from the web and write: MATCH (e:Event) WHERE id(e) = 23 RETURN e , yes it returns the node I want, but when I do it through the Doctrine repository it does not work. This is my code:

$entityManager = EntityManager::create('bolt://neo4j:1234567@neo4j:7687');
        $repository = $entityManager->getRepository(Event::class);
        /**@var Event $event*/
        $event = $repository->findOneBy(['id' => 23]);

        if ($event === null) {
            echo 'event not found' . PHP_EOL;
            exit(1);
        }

        echo sprintf("linkId:- %s; externalId: %s; id:%d\n", $event->getLinkId(), $event->getExternalId(), $event->getId());

this is the Event entity:

/**
 *
 * @OGM\Node(label="Event")
 */
class Event implements ToArrayInterface
{        
    /**
     * @var int
     *
     * @OGM\GraphId()
     */
    protected $id;

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

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

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

If instead of filtering the property ' id ' filter by the property 'name' , the node returns, that's why I know that node has the id = 23, however, when you filter by the id, it does not find the node, it returns null and therefore 'event not found' .

    
asked by Jaime Roman 16.04.2018 в 20:27
source

1 answer

1

In Neo4j the id of the Nodes is given a different treatment than the rest of the properties, for example, suppose our nodes have properties id, name, owner strong>, if we want to look for the events that have as owner to Pepe , we type: Match (e:Event) where e.propietario = "pepe" Return e , Or what is the same: Match (e:Event{propietario:"pepe"}) Return e , from the repository would be:

$repository = $entityManager->getRepository(Event::class);
        /**@var Event $event*/
        $event = $repository->findOneBy(['propietario' => "pepe"]);

However, if we want to return the event that has id , the value 5 , it is not enough to change the owner property to < strong> id , in that case the query would be: Match (e:Event) where id(5)=1 Return e , for this reason if we are going to run from the repository, it would not be enough to use the findOneBy method, this is the one used for common properties, for the id you have to use findOneById :

$repository = $entityManager->getRepository(Event::class);
            /**@var Event $event*/
            $event = $repository->findOneById(['id' => 5]);
    
answered by 23.04.2018 / 20:47
source