使用 lumen 检索边和关系属性
Retrieve edge and relationship properties using lumen
我是 lumen 和 neo4j 的新手,我想为两个标签之间的特定关系添加一些属性。
例如,如果我们假设 Exhibit 标签和 Zone 标签之间存在多对多关系,以坐标为特征属性,如何获取坐标属性?
阅读 neoEloquent documentation,我了解到要检索两个标签之间的边缘,我必须遵循函数 找到区域:
<?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}
"}
您的 PHP 代码是正确的,但是 Neo4j 区分大小写,请尝试将 $this->belongsToMany('App\Zone', 'BELONGS_TO');
更改为 $this->belongsToMany('App\Zone', 'belongs_to');
是的,您的代码会转换为您问题中的查询。
编辑
对于您遇到的错误,请尝试使用 $exhibit->zones()->edge($exhibit->zones)
而不是 $exhibit->zones()->edge()
如果 $exhibit->zones
的 returned 值是一个数组,这将不起作用,您将拥有迭代此数组或在关系的另一端指定特定模型,即:
检索所有边:
$edges = array();
foreach($exhibit->zones as $zone) {
$edge = $exhibit->zones()->edge($zone);
array_push($edges, $edge);
}
或者检索特定节点的边,其中 <id>
需要替换为关系另一端的 id,或者您可以指定另一个搜索函数 return 1 个展品对象:
$edge = $exhibit->zones()->edge(Exhibit::find(<id_of_related_node>));
我是 lumen 和 neo4j 的新手,我想为两个标签之间的特定关系添加一些属性。
例如,如果我们假设 Exhibit 标签和 Zone 标签之间存在多对多关系,以坐标为特征属性,如何获取坐标属性?
阅读 neoEloquent documentation,我了解到要检索两个标签之间的边缘,我必须遵循函数 找到区域:
<?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}
"}
您的 PHP 代码是正确的,但是 Neo4j 区分大小写,请尝试将 $this->belongsToMany('App\Zone', 'BELONGS_TO');
更改为 $this->belongsToMany('App\Zone', 'belongs_to');
是的,您的代码会转换为您问题中的查询。
编辑
对于您遇到的错误,请尝试使用 $exhibit->zones()->edge($exhibit->zones)
而不是 $exhibit->zones()->edge()
如果 $exhibit->zones
的 returned 值是一个数组,这将不起作用,您将拥有迭代此数组或在关系的另一端指定特定模型,即:
检索所有边:
$edges = array();
foreach($exhibit->zones as $zone) {
$edge = $exhibit->zones()->edge($zone);
array_push($edges, $edge);
}
或者检索特定节点的边,其中 <id>
需要替换为关系另一端的 id,或者您可以指定另一个搜索函数 return 1 个展品对象:
$edge = $exhibit->zones()->edge(Exhibit::find(<id_of_related_node>));