sgpatil / oriquent

Orientdb Eloquent driver for Laravel 5
MIT License
47 stars 15 forks source link

How to insert or update node with JSON data. #24

Closed nambhd closed 8 years ago

nambhd commented 8 years ago

In OrientDB, I define a vertex CustomFields with 'fields' property is EMBEDEDLIST. But when I insert or update record, it considered as STRING. So, I modified your source following bellow: 1.Modify Sgpatil\Orientdb\Query\Grammars\Grammar::parameter() function for update:

public function parameter($value)
    {

        ...

        if (is_string($property) && is_array(json_decode($property, true)) && (json_last_error() == JSON_ERROR_NONE))
            return $property;

        return "'" . $property . "'";

    }
  1. Modify Sgpatil\Orientdb\Query\Grammars\CypherGrammar::compileInsert() function for insert:
public function compileInsert(Builder $query, array $values)
    {

...

        foreach ($values as $value) {
            $col_values = [];
            foreach ($value as $val) {
                if (is_bool($val)) {
                    // For the boolean column this need to be un-quote unless orientdb will reject
                    $col_values[] = $val ? 'true' : 'false';
                } else if(is_string($val) && is_array(json_decode($val, true)) && (json_last_error() == JSON_ERROR_NONE)){
                    // For json value
                    $col_values[] = $val;
                } else {
                    // Escape variable for security
                    $val = str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $val);
                    $col_values[] = "'" . $val . "'";
                }
            }
            $rows[] = implode(',', $col_values);
        }
        $parameters = "(" . implode('), (', $rows) . ")";

        return "insert into $table ($columns) values $parameters";
    }

It's right? I need help soon.

pin-cnx commented 8 years ago

you should folk this git then submit as merge request so we can see the changed.