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

使用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
    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
    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);
            }
        }
    }
    
    点赞 评论

相关推荐