我是lumen和neo4j的新手,我想为两个标签之间的特定关系添加一些属性。
例如,如果我们假设 Exhibit 标签和 Zone 标签之间以坐标属性为特征存在多对多关系,我该如何检索坐标属性?
阅读 neoEloquent documentation后,我了解到要检索两个标签之间的边缘,我必须遵循一个过程,就像函数 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}
"}
答案 0 :(得分:1)
您的PHP代码正确,但是Neo4j区分大小写,请尝试将$this->belongsToMany('App\Zone', 'BELONGS_TO');
更改为$this->belongsToMany('App\Zone', 'belongs_to');
是的,您的代码将转换为您问题中的查询。
编辑
对于错误,您尝试使用$exhibit->zones()->edge($exhibit->zones)
而不是$exhibit->zones()->edge()
,如果返回的$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>));