duanjianlu0506 2013-06-28 12:33
浏览 281
已采纳

使用PHP中的neo4j-OGM为neo4j数据库中的关系添加属性

while we are able to create new nodes etc., we are still struggling to find out how properties can be added to existing relationships. For instance we are declaring the following in an Entity format:

   /**
     * @OGM\ManyToMany(relation="GOES_TO_MARKET")
     */
    protected $shoppers;

How do we make it so that we can add additional properties to GOES_TO_MARKET using doctrine format?

Thanks

  • 写回答

2条回答 默认 最新

  • douba9425 2013-06-28 12:56
    关注

    There is no direct way to do it through the API at this time. While there is some work in progress, it is quite far from completed.

    You can register a callback on relation creation.

    $em->registerEvent(HireVoice\Neo4j\EntityManager::RELATION_CREATE, function ($type, $relationName, $from, $to, $relation) {
        // $relation is the Everyman\Neo4j\Relationship
        if ($relationName === 'GOES_TO_MARKET') {
            $relation->setProperty('foobar', 'baz')->save();
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douke6424 2014-04-01 14:20
    关注

    Noticed that the solution posted by Louis-Philippe, after a recent installation, failed to work because the triggerEvent method under the EntityManager class is calling array_shift on the arguments provided. This therefore led to the callback only receiving four of the five required parameters.

    $em->registerEvent(HireVoice\Neo4j\EntityManager::RELATION_CREATE, function ($type, $relationName, $from, $to, $relation) {
        // $relation is the Everyman\Neo4j\Relationship
        if ($relationName === 'GOES_TO_MARKET') {
            $relation->setProperty('foobar', 'baz')->save();
        }
    });
    

    This was fixed by replacing the following: (Note that this hasn't been thoroughly tested and might impact some other parts of the code.)

    private function triggerEvent($eventName, $data)
    {
        if (isset($this->eventHandlers[$eventName])) {
            $args = func_get_args();
            array_shift($args);
    
            foreach ($this->eventHandlers[$eventName] as $callback) {
                $clone = $args;
                call_user_func_array($callback, $clone);
            }
        }
    }
    

    by:

    private function triggerEvent($eventName, $data)
    {
        if (isset($this->eventHandlers[$eventName])) {
            $args = func_get_args();
    
            foreach ($this->eventHandlers[$eventName] as $callback) {
                $clone = $args;
                call_user_func_array($callback, $clone);
            }
        }
    }
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 正常上网,内部网页无法打开
  • ¥15 组件库引入并使用在若依框架未展示
  • ¥149 关于#使用python 的Flash Echarts+ajax+mysql动态数据实现饼图#的问题,请各位专家解答!
  • ¥15 RichTextBox中追加文本时报错
  • ¥15 关于c语言的学习问题
  • ¥15 activity升级到flowable工作流act_ge_bytearray的草稿json数据复制到act_de_model 的model_editor_json的脚本
  • ¥15 cvi使用CreateThread创建线程时,出现存储空间不足无法处理此命令的错误
  • ¥15 求苹果推信imessage批量推信技术
  • ¥15 ubuntu 22.04 系统盘空间不足。隐藏的docker空间占用?(相关搜索:移动硬盘|管理系统)
  • ¥15 c++ word自动化,为什么可用接口是空的?