arangodb-php icon indicating copy to clipboard operation
arangodb-php copied to clipboard

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

Open karvex opened this issue 3 years ago • 3 comments
trafficstars

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

I am using the following versions:

  • php v7.4.26
  • arangodb v3.9
  • triagens/arangodb v3.6.0
$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?

karvex avatar Mar 10 '22 16:03 karvex

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

krkrTac avatar Apr 15 '22 10:04 krkrTac

Apparently this is still broken a year later:

  • php v8.0
  • arangodb v3.9.4
  • triagens/arangodb v3.8.0

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.

mtzonev avatar Feb 24 '23 11:02 mtzonev

@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();

jsteemann avatar Feb 24 '23 13:02 jsteemann