arangodb / arangodb-php

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

Accessing keys and ids from cursor #239

Closed spiro-stathakis closed 6 years ago

spiro-stathakis commented 6 years ago

Hi there,

I am trying to access the keys and ids from an AQL query but not having any joy. All the docs use var_dump() to show their data but this is not very useful when trying to work within a non debugging context. I have tried a number of looping constructs to extract the id and key from the cursor but I keep getting nulls. Please have a look at this and let me know how it can improve:

/* pseudo code to get a cursor */ 
$cursor =  'FOR u IN users RETURN u'; 

       while($cursor->valid() === true) {
           echo sprintf('user_id %s, id=%s , key=%s <br/>', 
                             $cursor->current()->client_id ,
                             $cursor->current()->key, 
                             $cursor->current()->id 
              ); 
           $cursor->next(); 

       }

OR

    foreach($cursor->getAll() as $c) 
           echo sprintf('user_id= %s, id=%s , key=%s <br/>', $c->client_id ,$c->key, $c->id ); 
spiro-stathakis commented 6 years ago

Update: I have been using id instead of getId() so this works fine:

foreach($cursor->getAll() as $c) 
           echo sprintf('user_id= %s, id=%s , key=%s <br/>', $c->client_id ,$c->getId() ); 
Jorgebv02 commented 5 years ago

I know it's been over a year since this issue was closed, but for someone that is like me struggling how to solve this, you just have to use the function get that is included in the ArangoDocument. I.e. $resultingDocuments[$key]->get('username')

sonyarianto commented 5 years ago

and how to get the _key? I can get the _id using ->getId() method, how to get the _key ?

UPDATE: Ooops sorry hahaha, I found it, use ->getInternalKey();

jsteemann commented 5 years ago

@sonyarianto : sorry, just saw it right now. There are 2 ways of accessing the key:

via getInternalKey:

$statement = new Statement($connection, [ 'query' => $query ]);
$cursor = $statement->execute();

foreach ($cursor->getAll() as $doc) {
  var_dump($doc->getInternalKey());
}

via array access (requires _flat attribute set in the statement):

$statement = new Statement($connection, [ 'query' => $query, '_flat' => true ]);
$cursor = $statement->execute();

foreach ($cursor->getAll() as $doc) {
  var_dump($doc['_key']);
}
sonyarianto commented 5 years ago

@jsteemann thank you very much, it's clear now :)