1

我是 lumen 和 neo4j 的新手,我想为两个标签之间的特定关系添加一些属性。

例如,如果我们假设Exhibit标签和以坐标属性为特征的区域标签之间存在多对多关系,我如何检索坐标属性?

阅读neoEloquent 文档,我了解到要检索两个标签之间的边缘,我必须遵循函数findZone中存在的过程:

<?php

    namespace App;

    use Vinelab\NeoEloquent\Eloquent\Model;

    class Exhibit extends Model{
        protected $label = 'Exhibit';

        protected $fillable = [];

        protected $hidden = [];

        public function zones(){
            return $this->belongsToMany('App\Zone', 'BELONGS_TO');
        }

        public static function findZone($exhibit_id){
            $exhibit = Exhibit::find($exhibit_id);
            $exhibit->zones();


            return $exhibit->zones()->edge();
        }
    }

问题是,当我测试这个函数时,结果是一个空响应,但我确信标签之间有一个边缘。

我错了还是 findZone 函数可以在 Cypher 中翻译为这个查询?

MATCH (e:Exhibit)-[edge:belongs_to]->(z:Zone) WHERE e.exhibit_id = {exhibit_id} RETURN edge 

编辑

正如用户 Sven Hakvoort 建议我的那样,我有改变

$this->belongsToMany('App\Zone', 'BELONGS_TO'); to $this->belongsToMany('App\Zone', 'belongs_to');

现在响应是 500 内部错误,堆栈跟踪:

    [2018-10-13 09:26:15] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Argument 2 passed to Vinelab\NeoEloquent\Eloquent\Edges\Finder::first() must be an instance of Vinelab\NeoEloquent\Eloquent\Model, null given, called in E:\laravel-projects\api_certose\vendor\vinelab\neoeloquent\src\Eloquent\Relations\BelongsToMany.php on line 158 in E:\laravel-projects\api_certose\vendor\vinelab\neoeloquent\src\Eloquent\Edges\Finder.php:70
Stack trace:
#0 E:\laravel-projects\api_certose\vendor\vinelab\neoeloquent\src\Eloquent\Relations\BelongsToMany.php(158): Vinelab\NeoEloquent\Eloquent\Edges\Finder->first(Object(App\Exhibit), NULL, 'belongs_to')
#1 E:\laravel-projects\api_certose\app\Exhibit.php(21): Vinelab\NeoEloquent\Eloquent\Relations\BelongsToMany->edge()
#2 E:\laravel-projects\api_certose\app\Http\Controllers\ExhibitController.php(44): App\Exhibit::findZone('159')
#3 [internal function]: App\Http\Controllers\ExhibitController->retrieveZone('159')
#4 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(29): call_user_func_array(Array, Array)
#5 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#6 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Laravel\Lumen\Application), Array, Object(Closure))
#7 E:\laravel-projects\api_certose\vendor\illuminate\container\Container.php(564): Illuminate\Container\BoundMethod::call(Object(Laravel\Lumen\Application), Array, Array, NULL)
#8 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(373): Illuminate\Container\Container->call(Array, Array)
#9 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(339): Laravel\Lumen\Application->callControllerCallable(Array, Array)
#10 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(313): Laravel\Lumen\Application->callLumenController(Object(App\Http\Controllers\ExhibitController), 'retrieveZone', Array)
#11 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(275): Laravel\Lumen\Application->callControllerAction(Array)
#12 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(260): Laravel\Lumen\Application->callActionOnArrayBasedRoute(Array)
#13 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(230): Laravel\Lumen\Application->handleFoundRoute(Array)
#14 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(164): Laravel\Lumen\Application->handleDispatcherResponse(Array)
#15 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(413): Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}()
#16 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(166): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#17 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(107): Laravel\Lumen\Application->dispatch(NULL)
#18 E:\laravel-projects\api_certose\public\index.php(28): Laravel\Lumen\Application->run()
#19 {main} {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Argument 2 passed to Vinelab\\NeoEloquent\\Eloquent\\Edges\\Finder::first() must be an instance of Vinelab\\NeoEloquent\\Eloquent\\Model, null given, called in E:\\laravel-projects\\api_certose\\vendor\\vinelab\
eoeloquent\\src\\Eloquent\\Relations\\BelongsToMany.php on line 158 at E:\\laravel-projects\\api_certose\\vendor\\vinelab\
eoeloquent\\src\\Eloquent\\Edges\\Finder.php:70)
[stacktrace]
#0 E:\\laravel-projects\\api_certose\\vendor\\vinelab\
eoeloquent\\src\\Eloquent\\Relations\\BelongsToMany.php(158): Vinelab\\NeoEloquent\\Eloquent\\Edges\\Finder->first(Object(App\\Exhibit), NULL, 'belongs_to')
#1 E:\\laravel-projects\\api_certose\\app\\Exhibit.php(21): Vinelab\\NeoEloquent\\Eloquent\\Relations\\BelongsToMany->edge()
#2 E:\\laravel-projects\\api_certose\\app\\Http\\Controllers\\ExhibitController.php(44): App\\Exhibit::findZone('159')
#3 [internal function]: App\\Http\\Controllers\\ExhibitController->retrieveZone('159')
#4 E:\\laravel-projects\\api_certose\\vendor\\illuminate\\container\\BoundMethod.php(29): call_user_func_array(Array, Array)
#5 E:\\laravel-projects\\api_certose\\vendor\\illuminate\\container\\BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#6 E:\\laravel-projects\\api_certose\\vendor\\illuminate\\container\\BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Laravel\\Lumen\\Application), Array, Object(Closure))
#7 E:\\laravel-projects\\api_certose\\vendor\\illuminate\\container\\Container.php(564): Illuminate\\Container\\BoundMethod::call(Object(Laravel\\Lumen\\Application), Array, Array, NULL)
#8 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(373): Illuminate\\Container\\Container->call(Array, Array)
#9 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(339): Laravel\\Lumen\\Application->callControllerCallable(Array, Array)
#10 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(313): Laravel\\Lumen\\Application->callLumenController(Object(App\\Http\\Controllers\\ExhibitController), 'retrieveZone', Array)
#11 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(275): Laravel\\Lumen\\Application->callControllerAction(Array)
#12 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(260): Laravel\\Lumen\\Application->callActionOnArrayBasedRoute(Array)
#13 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(230): Laravel\\Lumen\\Application->handleFoundRoute(Array)
#14 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(164): Laravel\\Lumen\\Application->handleDispatcherResponse(Array)
#15 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(413): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}()
#16 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(166): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))
#17 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(107): Laravel\\Lumen\\Application->dispatch(NULL)
#18 E:\\laravel-projects\\api_certose\\public\\index.php(28): Laravel\\Lumen\\Application->run()
#19 {main}
"} 
4

1 回答 1

1

您的 PHP 代码是正确的,但是 Neo4j 区分大小写,请尝试更改$this->belongsToMany('App\Zone', 'BELONGS_TO');$this->belongsToMany('App\Zone', 'belongs_to');

是的,您的代码转换为您问题中的查询。

编辑

对于您遇到的错误,如果返回的值是数组,则 try$exhibit->zones()->edge($exhibit->zones)而不是$exhibit->zones()->edge()This will not work $exhibit->zones,您将不得不遍历此数组或在关系的另一端指定特定模型,即:

要检索所有边:

$edges = array();
foreach($exhibit->zones as $zone) {
    $edge = $exhibit->zones()->edge($zone);
    array_push($edges, $edge);
}

或者检索特定节点的边缘,其中<id>需要替换为关系另一侧的 id,或者您可以指定另一个搜索函数,该函数将返回 1 个 Exhibit 对象:

 $edge = $exhibit->zones()->edge(Exhibit::find(<id_of_related_node>));
于 2018-10-12T14:26:58.450 回答