Vinelab / NeoEloquent

The Neo4j OGM for Laravel
MIT License
633 stars 197 forks source link

The problem with the function modelAsNode handling Chinese strings #299

Closed genggui001 closed 3 years ago

genggui001 commented 5 years ago
$firstChar = substr($labels, 0, 1);
$suffix = substr($labels, 1, strlen($labels) - 1);
$labels = mb_strtolower($firstChar) . $suffix;

Cutting the Chinese string with the substr() and stitching it back will cause garbled characters. Use lcfirst() to solve the problem $labels = lcfirst($labels);

holix commented 5 years ago

Hey @genggui001, you can extend NeoEloquent:

AppServiceProvider.php

<?php

namespace App\Providers;

use App\Grammar;
use Illuminate\Support\ServiceProvider;
use Vinelab\NeoEloquent\Connection;
use Vinelab\NeoEloquent\Schema\Grammars\CypherGrammar;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
       //
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app['db']->extend('neo4j', function($config)
        {
            $conn = new Connection($config);
            $conn->setQueryGrammar(new Grammar());
            $conn->setSchemaGrammar(new CypherGrammar());
            return $conn;
        });
    }
}

Grammar.php

<?php 

namespace App\Database;

use Vinelab\NeoEloquent\Query\Grammars\CypherGrammar;

class Grammar extends CypherGrammar
{
    public function modelAsNode($labels = null, $relation = null)
    {
        if ($labels === null) {
            return 'n';
        }

        if ($relation !== null) {
            $labels = 'with_' . $relation . '_' . $labels;
        }

        if (is_array($labels)) {
            return mb_strtolower(array_last($labels));
        }

        return mb_strtolower($labels);
    }
}

You can modify modelAsNode so it fits your needs.

stale[bot] commented 3 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.