简体   繁体   English

如何在neo4jphp中实现标签的自动递增id属性?

[英]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". 我必须创建模式,以使标签“城市”具有属性“ id”。 I have to make autoincrement value of "id" on every node I create of label type "City". 我必须在我创建的标签类型为“城市”的每个节点上使“ id”的自动递增值。 I am using PHP library of neo4j: 我正在使用neo4j的PHP库:

I have seen this task Auto Increment in Neo4j but it is not having way given to do by php or specific strategy. 我已经在Neo4j中看到了此任务Auto Increment,但是它没有通过php或特定策略来完成。

您可以使用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 从技术上讲,这不是您要的(因为它不是自动增量或数字),因为neo4j没有自动增量...但是这就是我通过生成一个随机字符串来解决相同的问题,因为一个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... 如果您想要不同的默认长度,请更改$length$tries仅用于防止无限循环...(此函数尝试创建5次长度的唯一ID 3次,然后尝试创建6个长id等...

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. 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. 我在代码中删除了抽象层,并用实际的neo4jphp lib代码替换了它,但是我尚未对其进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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