Vinelab / NeoEloquent

The Neo4j OGM for Laravel
MIT License
634 stars 200 forks source link

How to use NeoEloquent outside of Laravel? #165

Closed ghost closed 4 years ago

ghost commented 8 years ago

Is there a way to use NeoEloquent outside of Laravel, and if so, how?

Specifically, how do I instantiate things and define configuration so that I can use the Eloquent class for designing model classes?

(There are a few resources on how to use Illuminate outside of Laravel, but they're quite old and I haven't been able to get them to work.)

netpoe commented 7 years ago

Well, here's my approach, I'm trying to do the same.

Everything seemed fine until an exception regarding the connection with the real Eloquent Model.

My first steps:

composer require vinelab/neoeloquent

Then, I proceeded to do the connection:

<?php 

namespace Elang;

use Vinelab\NeoEloquent\Connection;

class Application
{
    protected $connection;

    public function run()
    {
        $this->connect();

        return $this;
    }

    public function connect()
    {
        $databaseConfig = dirname(__DIR__, 1) . '/config/database.php';
        $connections = require($databaseConfig);
        $neo4j = $connections['connections']['neo4j'];
        $connection = new Connection($neo4j);

        try {
            $connection->createConnection();
            $this->connection = $connection->getClient();
        } catch (\Exception $e) {
            error_log($e->getMessage());
            die();
        }

        return $this;
    }

    public function getConnection()
    {
        return $this->connection;
    }
}

Where the DB config is config/database.php:

<?php 

return [
    'connections' => [
        'neo4j' => [
            'host'   => 'localhost',
            'port'   => '7687',
            'username' => null,
            'password' => null,
        ]
    ]
];

And later run the application:

$app = new Elang\Application;
$app->run();

The connection didn't throw an exception so I assumed it was cool.

However, when defining a Model:

<?php 

namespace Elang\Model;

use Vinelab\NeoEloquent\Eloquent\Model;

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

    protected $fillable = ['text'];
}

I wonder if this is the right Model to extend from.

I'm using it as:

$word = \Elang\Model\Word::create(['text' => 'word']);

But I'm getting an exception:

Fatal error: Uncaught Error: Call to a member function connection() on null in ~/elangapi/vendor/illuminate/database/Eloquent/Model.php:3339
Stack trace:
#0 ~/elangapi/vendor/illuminate/database/Eloquent/Model.php(3305): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)
#1 ~/elangapi/vendor/vinelab/neoeloquent/src/Vinelab/NeoEloquent/Eloquent/Model.php(67): Illuminate\Database\Eloquent\Model->getConnection()
#2 ~/elangapi/vendor/illuminate/database/Eloquent/Model.php(1832): Vinelab\NeoEloquent\Eloquent\Model->newBaseQueryBuilder()
#3 ~/elangapi/vendor/illuminate/database/Eloquent/Model.php(1451): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes()
#4 ~/elangapi/vendor/illuminate/database/Eloquent/Model.php(562): Illuminate\Database\Eloquent\Model->save()
#5 ~/elangapi/src/Scraper/es_M in ~/elangapi/vendor/illuminate/database/Eloquent/Model.php on line 3339

Hope it gives you some hints and maybe you've tried a different approach that is working

netpoe commented 7 years ago

Well, finally I found a solution inspired by the tests.

It's not using Eloquent at all, so I cannot create Models, but it's using NeoEloquent:

public function createWord()
    {
        $connection = $this->app->getConnection();

        $create = 'CREATE (w:Word {word: {word}})';
        $cypher = $connection->getCypherQuery($create, [
            ['word' => 'gato'],
        ]);

        $results = $cypher->getResultSet();
    }

The $connection = $this->app->getConnection(); line contains the following:

public function connect()
    {
        $databaseConfig = dirname(__DIR__, 1) . '/config/database.php';
        $connections = require($databaseConfig);
        $neo4j = $connections['connections']['neo4j'];

        try {
            $this->connection = new Connection($neo4j);
        } catch (\Exception $e) {
            error_log($e->getMessage());
            die();
        }

        return $this;
    }

And the config/database.php file is using the defaults

<?php 

return [
    'connections' => [
        'neo4j' => [
            'driver' => 'neo4j',
            'host'   => 'localhost',
            'port'   => '7474',
            'username' => 'neo4j',
            'password' => 'neo',
            'path' => '/data',
        ]
    ]
];
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.