简体   繁体   中英

How to implement auto-increment id property of label in neo4jphp?

I have to create schema such that there will be property "id" for label "City". I have to make autoincrement value of "id" on every node I create of label type "City". I am using PHP library of neo4j:

I have seen this task Auto Increment in Neo4j but it is not having way given to do by php or specific strategy.

您可以使用Java(或其他JVM语言)编写不受管理的扩展 ,该扩展使用TransactionEventHandler来管理计数器并将其部署到Neo4j服务器。

This isn't technically what you asked for (since it isn't an auto-increment or numeric) since neo4j doesn't have an auto-increment... but this is how I solved the same problem by generating a randomized string as an ID

change $length if you want a different default length, $tries is just used to prevent an infinite loop... (this function tries to create a unique ID of 5 length 3 times, then tries to create 6 long id etc...

function makeNewID($client, $length = 5, $trys = 0)
   {
      //chars to pick from
      $charlist = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
      $strlen = strlen($charlist)-1;

      //alpha numeric generated in this for statement
      for ($newID = '', $i = 0; $i < $length; $newID .= $charlist[mt_rand(0, $strlen)], ++$i);
      //query the database to see if the 'new id exists'
      $query = new Everyman\Neo4j\Cypher\Query($client, 'MATCH (n{id:"'.$newID.'"}) RETURN n.id LIMIT 1');
      $result = $query->getResultSet();
      if(!empty($result[0]['x']->getProperty('id')))
      {
        $trys++;
        if($trys >= 3){$length++; $trys = 0;}
        $newID = makeNewID($Neo, $length, $trys);
      }
      return $newID;
   }

PS. I removed the abstraction layer in my code and replaced it with the actual neo4jphp lib's code, but i haven't tested it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM