arangodb / arangodb-php

PHP ODM for ArangoDB
https://www.arangodb.com
Apache License 2.0
182 stars 46 forks source link

Error: Trying to access array offset on value of type int #292

Open karvex opened 2 years ago

karvex commented 2 years ago

It works with ArangoDB Queries UI, but not with triagens/arangodb v3.6.0.

I am using the following versions:

$query = '
    FOR c in cars
        LET carSeats = (
            FOR s in c.seats
            RETURN s
        )
    RETURN carSeats';
$response = DB::statement($query, []);

Callstack: [2022-03-10 16:16:22] local.ERROR: Trying to access array offset on value of type int {"exception":"[object] (ErrorException(code: 0): Trying to access array offset on value of type int at /var/www/api/repository/vendor/triagens/arangodb/lib/ArangoDBClient/Document.php:299) [stacktrace]

0 /var/www/api/repository/vendor/triagens/arangodb/lib/ArangoDBClient/Document.php(299): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Trying to acces...', '/var/www/api/re...', 299, Array)

1 /var/www/api/repository/vendor/triagens/arangodb/lib/ArangoDBClient/Document.php(179): ArangoDBClient\Document->set(0, Array)

2 /var/www/api/repository/vendor/triagens/arangodb/lib/ArangoDBClient/Cursor.php(470): ArangoDBClient\Document::createFromArray(Array, Array)

3 /var/www/api/repository/vendor/triagens/arangodb/lib/ArangoDBClient/Cursor.php(408): ArangoDBClient\Cursor->addDocumentsFromArray(Array)

4 /var/www/api/repository/vendor/triagens/arangodb/lib/ArangoDBClient/Cursor.php(227): ArangoDBClient\Cursor->add(Array)

5 /var/www/api/repository/vendor/triagens/arangodb/lib/ArangoDBClient/Statement.php(321): ArangoDBClient\Cursor->__construct(Object(ArangoDBClient\Connection), Array, Array)

6 /var/www/api/repository/app/Arangodb/Connection.php(204): ArangoDBClient\Statement->execute()

7 /var/www/api/repository/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php(388): App\Arangodb\Connection->statement('\

        FO...')

8 /var/www/api/repository/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(261): Illuminate\Database\DatabaseManager->__call('statement', Array)

9 /var/www/api/repository/app/Repositories/Listing.php(257): Illuminate\Support\Facades\Facade::__callStatic('statement', Array)

Anyone having the same problem and found a solution?

krkrTac commented 2 years ago

Hi @karvex \ \ I have maybe the same issue when I want to access a specific key in document like RETURN DOCUMENT('collection/_key').myKey or FOR doc IN collection FILTER doc._key == '_key' RETURN doc.myKey \ PHP launch NOTICE : \ Trying to access array offset on value of type int in .../vendor/triagens/arangodb/lib/ArangoDBClient/Document.php on line 304 \ I fix temporary the issue when I check $key is a string \ if (is_string($key) && $key[0] === '_') { But I think is not enough to fix your issue

mtzonev commented 1 year ago

Apparently this is still broken a year later:

It seems to happen when the resultset is a generic array of other arrays or objects and not native documents.

I came up with a very similar fix to the one posted by @krkrTac.

jsteemann commented 1 year ago

@mtzonev : The driver tries to turn the query result into an array of Document objects by default. This will not work if the returned data aren't actually documents. In this case, please try setting the _flat option of the statement as follows:

        $statement = new Statement($connection, ['_flat' => true]);
        $statement->setQuery('RETURN (FOR i IN 1..1000 RETURN CONCAT("test", i))');
        $cursor = $statement->execute();