我想使用lumen创建一个api rest,它将与neo4j通信,为此,我正在使用NeoEloquent。 我已经阅读了NeoEloquent的文档,但感到困惑。我已经了解了流明如何与关系数据库一起使用,有一个模型,一个控制器,我想对数据库执行的每个操作都经过一个路由,该路由指定了要使用的方法,但是我不明白使用图形数据库。 特别是我不理解如何使用Http方法创建新标签,检索所有标签和关系。 我尝试遵循与guide中说明的相同过程(显然已将其重新适应我的用例),但没有成功。
让我们说我们有两个具有多对多关系的标签,这个标签将是“展览”和“区域”。我们要检索与具有特定标识的展览关联的区域。 因此,查询将如下所示:
MATCH (e:Exhibit)-[belongs_to]->(z:Zone) WHERE e.exhibit_id = {exhibit_id} RETURN z
为此查询,我们需要具有必须在 web.php 文件中显示的以下路由:
$router->get('/', function () use ($router) {
return $router->app->version();
});
$router->group(['prefix' => 'api'], function () use ($router) {
$router->get('exhibit', ['uses' => 'ExhibitController@showAllExhibit']);
$router->get('exhibit/{exhibit_id}', ['uses' => 'ExhibitController@retrieveZone']);
});
通过这条路线,我们说: 当带有 get 方法的请求出现时,进入 ExhibitController 类内部并调用 retrieveZone 函数。 这就是控制器类中的内容:
<?php
namespace App\Http\Controllers;
use App\Exhibit;
use Illuminate\Http\Request;
class ExhibitController extends Controller
{
public function showAllExhibit()
{
return response()->json(Exhibit::all());
}
public function showOneExhibit($id)
{
return response()->json(Exhibit::find($id));
}
public function create(Request $request)
{
$exhibit = Exhibit::create($request->all());
return response()->json($exhibit, 201);
}
public function update($id, Request $request)
{
$exhibit = Exhibit::findOrFail($id);
$exhibit->update($request->all());
return response()->json($exhibit, 200);
}
public function delete($id)
{
Exhibit::findOrFail($id)->delete();
return response('Deleted Successfully', 200);
}
public function retrieveZone($exhibit_id)
{
$result = Exhibit::findZone($exhibit_id);
return response()->json($result,201);
}
}
当调用 retrieveZone 函数时,我们还将调用展示模型中存在的函数 findZone :
<?php
namespace App;
use Vinelab\NeoEloquent\Eloquent\Model;
use Vinelab\NeoEloquent\Facade\Neo4jSchema;
class Exhibit extends Model{
protected $label = 'Exhibit';
protected $fillable = [];
protected $hidden = [];
public function belongsToManyZone(){
return $this->belongsToMany('Zone', 'belongs_to');
}
public static function findZone($exhibit_id){
$exhibit = Exhibit::find($exhibit_id);
return $exhibit->belongsToManyZone();
}
}
区域类:
<?php
namespace App;
use Vinelab\NeoEloquent\Eloquent\Edges\EdgeIn;
use Vinelab\NeoEloquent\Eloquent\Model;
class Zone extends Model{
protected $label = 'Zone';
protected $fillable = ['name'];
protected $hidden = [];
}
这是我使用NeoEloquent,Lumen和Fastroute转换查询的结果,但是结果是 500 Internal Server Error 。
堆栈跟踪
[2018-10-11 16:37:18] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Class 'Zone' not found in E:\laravel-projects\api_certose\vendor\vinelab\neoeloquent\src\Eloquent\Model.php:291
Stack trace:
#0 E:\laravel-projects\api_certose\app\Exhibit.php(16): Vinelab\NeoEloquent\Eloquent\Model->belongsToMany('Zone', 'BELONGS_TO')
#1 E:\laravel-projects\api_certose\app\Exhibit.php(22): App\Exhibit->zones()
#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): Class 'Zone' not found at E:\\laravel-projects\\api_certose\\vendor\\vinelab\
eoeloquent\\src\\Eloquent\\Model.php:291)
[stacktrace]
#0 E:\\laravel-projects\\api_certose\\app\\Exhibit.php(16): Vinelab\\NeoEloquent\\Eloquent\\Model->belongsToMany('Zone', 'BELONGS_TO')
#1 E:\\laravel-projects\\api_certose\\app\\Exhibit.php(22): App\\Exhibit->zones()
#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)
为了使NeoEloquent在模型之间建立连接,您还需要定义“相关”模型,用$this->belongsToMany('Zone', 'belongs_to');
行指定该类与另一个类(即图数据库中的节点)相关)并与一个关系有关。
为了解决您的问题,您需要至少使用以下内容指定此类:
<?php
namespace App;
use Vinelab\NeoEloquent\Eloquent\Model;
use Vinelab\NeoEloquent\Facade\Neo4jSchema;
class Zone extends Model{}
我还建议将belongsToManyZone()
重命名为zones()
以提高代码的可读性,因为这样您就可以执行以下操作来获得Zones
中所有Exhibit
的内容:
$result = Exhibit::findZone($exhibit_id);
$zones = $result->zones
否则,$result->belongsToManyZone
会显得有点奇怪。
还尝试更改$this->belongsToMany('App\Zone', 'belongs_to');
,因为这将确保使用正确的名称空间和类,并且可以找到它们。
我希望这可以解决您的问题