سؤال

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

هل كانت مفيدة؟

المحلول

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();
    }
});

نصائح أخرى

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);
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top