Search code examples
phpencodingpdoneo4jneo4jphp

Neo4j encoding issue (I suspect) with UTF-8 Characters using Neo4jphp


I'm attempting a simple extraction from a UTF-8 encoded MySQL table of rows and creating nodes from them. It works fine until it hits a row with entry of name of 'Country House Café'.

I'm using the neo4jphp wrapper library for the REST API.

I pull rows using a basic PDO select:

 $results = $db->query("SELECT * FROM table WHERE name is not null");  
 $rows = $results->fetchAll(PDO::FETCH_ASSOC);

I then loop through the rows:

foreach($rows as $row){
    try {
       $node = $client->makeNode($row);
       $node->save();
    } catch (Exception $e) {
         var_dump($row); exit;
    }
}

It happily iterates through creating and saving nodes until it hits one who's property 'name' is the string 'Country House Café'. That triggers an exception that looks like this

 Headers: Array
 (
     [Content-Type] =>  application/json; charset=UTF-8; stream=true
     [Access-Control-Allow-Origin] =>  *
     [Transfer-Encoding] =>  chunked
     [Server] =>  Jetty(9.0.5.v20130815)
 )
 Body: Array
 (
     [message] => Could not set property "name", unsupported type: null
     [exception] => PropertyValueException
     [fullname] => org.neo4j.server.rest.web.PropertyValueException
     [stacktrace] => Array
            (
                [0] => org.neo4j.server.rest.domain.PropertySettingStrategy.setProperty(PropertySettingStrategy.java:141)
                [1] => org.neo4j.server.rest.domain.PropertySettingStrategy.setProperties(PropertySettingStrategy.java:88)
                [2] => org.neo4j.server.rest.web.DatabaseActions.createNode(DatabaseActions.java:214)
                [3] => org.neo4j.server.rest.web.RestfulGraphDatabase.createNode(RestfulGraphDatabase.java:238)
                [4] => java.lang.reflect.Method.invoke(Method.java:483)
                [5] => org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)
                [6] => org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)
                [7] => java.lang.Thread.run(Thread.java:745)
            )

    )

Now, if I manually do something explictly like this in PHP, all works:

$node = $client->makeNode(array("name" => "Country House Café"));
$node->save();

Anyone have a clue what could be the culprit here?


Solution

  • Ok, egg on my face--figured it out, it was actually with my use of PDO.

    I'm now fully aware PDO doesn't default encoding to that of your DB, table, or just UTF8. I'm still not really sure where it picks the default encoding, and not feeling the urge to investigate this further.

    My original DSN was

       $dsn = "mysql:host=$hostname;dbname=$database";
       $db = new \PDO($dsn, $username, $password);
    

    I change it to add the DB.

      $dsn = "mysql:host=$hostname;dbname=$database;charset=utf8";   
    

    Bingo, magic.