Print ArangoDB data with PHP

238 views
Skip to first unread message

Михаил Ефимов

unread,
Feb 13, 2013, 2:16:09 PM2/13/13
to aran...@googlegroups.com
We are making an online shop using ArangoDB with ArangoDB-PHP, we have created a collection called 'products' with a key called 'name' and we extract one document with this line

$product = $handler->getByExample('products', array('name'=>'guitar_1'));

We tryed to put the information in an array using

$assoc["1"] = array("products_id"=>$product->idd, "product_name"=>$product->name, "price"=>$product->price);
 
and try to print the array, but we get the next error

TRACE FOR send string(183) "PUT /_api/simple/by-example HTTP/1.1 Host: localhost Content-Type: application/json Connection: Close Content-Length: 55 {"collection":"products","example":{"name":"guitar_1"}}" TRACE FOR receive string(318) "HTTP/1.1 201 Created server: triagens GmbH High-Performance HTTP Server connection: Close content-type: application/json; charset=utf-8 content-length: 155 {"result":[{"_id":"1468716785/1470355185","_rev":1470355185,"price":"2000","id":"1","name":"guitar_1"}],"hasMore":false,"count":1,"error":false,"code":201}" 
Notice: Undefined property: triagens\ArangoDb\Cursor::$idd in C:\xampp\htdocs\onlineshop\index.php on line 53

Notice: Undefined property: triagens\ArangoDb\Cursor::$name in C:\xampp\htdocs\onlineshop\index.php on line 53

Notice: Undefined property: triagens\ArangoDb\Cursor::$price in C:\xampp\htdocs\onlineshop\index.php on line 53

Frank Mayer

unread,
Feb 13, 2013, 3:59:09 PM2/13/13
to aran...@googlegroups.com
Seems that you found a small bug in the docs. In the docs it says that getByExample returns an array, but it's actually returning a cursor.

change your example above to:

$cursor= $handler->getByExample('products', array('name'=>'guitar_1'));

$product= $cursor->current();


Now you should be getting your document in form of an array.

I'll fix the docs.

However you shouldn't be using the DocumentHandler::getByExample() method, because it's deprecated in favor of the CollectionHandler::byExample() method. It will be removed in version 2.0 of the driver.

Jan Steemann

unread,
Feb 13, 2013, 4:12:39 PM2/13/13
to aran...@googlegroups.com, Frank Mayer
Thanks.
As far as I understood, in order for this to work no change in the
server is required, but it can be handled on the client-side/in the
driver only.
Is that correct?

Best regards
Jan
> *Notice*: Undefined property: triagens\ArangoDb\Cursor::$idd in
> *C:\xampp\htdocs\onlineshop\index.php* on line *53*
>
> *Notice*: Undefined property: triagens\ArangoDb\Cursor::$name in
> *C:\xampp\htdocs\onlineshop\index.php* on line *53*
>
> *Notice*: Undefined property: triagens\ArangoDb\Cursor::$price in
> *C:\xampp\htdocs\onlineshop\index.php* on line *53*
>
> --
> You received this message because you are subscribed to the Google
> Groups "ArangoDB" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to arangodb+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Frank Mayer

unread,
Feb 13, 2013, 4:18:56 PM2/13/13
to aran...@googlegroups.com, Frank Mayer, j.ste...@triagens.de
Yes, it's only a documentation issue. There is a test to getByExample in the testsuite which runs through nicely. No issues :)

Frank Mayer

unread,
Feb 13, 2013, 4:47:22 PM2/13/13
to aran...@googlegroups.com
A small correction to my previous reply.

$cursor= $handler->getByExample('products', array('name'=>'guitar_1'));

$product= $cursor->current();

This will give you a Document object where you can get the attributes like this:
$product->name

If you need the document in form of an array the you need to do:

$array = $product->getAll();
There are also some options to this. Please check the docs on this.


On Wednesday, February 13, 2013 9:16:09 PM UTC+2, Muumimojo wrote:

Frank Mayer

unread,
Feb 14, 2013, 4:09:41 AM2/14/13
to aran...@googlegroups.com
@Muumimojo

So, with this documentation fix I also implemented the firstExample() method into the master and devel branches, as well as 1.1 and 1.2 branches, 
With this you can now also get the first document by example, if you need to. Note, that this does return an object document instead of a cursor.

Muumimojo

unread,
Feb 26, 2013, 12:12:07 PM2/26/13
to aran...@googlegroups.com
We trying to make a recommendation system for customers, when a new order is placed. People who bought this product, also bought some another one and so on.

So we connected products and customers and in this edge collection we need to know which product belongs to which customer.

getByExample function returns an array, and how we can access the single id?

print_r($user_products); returns

Array ( [documents] => Array ( [0] => /_api/document/1547814530/1597584307 [1] => /_api/document/1547814530/1591794995 [2] => /_api/document/1547814530/1596339123 [3] => /_api/document/1547814530/1587928371 ) )

and if we try to print a single id it returns Undefined offset. Is there a way to print by key?

Frank Mayer

unread,
Feb 26, 2013, 12:27:25 PM2/26/13
to aran...@googlegroups.com
Hi,

From the print_r you showed, it seems that you have put the result of the cursor documents array into another array, so the way to access this would be $rootArray['documents'][0] or [1] etc...

Hope that helps,
Greeting Frank

Frank Mayer

unread,
Feb 26, 2013, 12:31:51 PM2/26/13
to aran...@googlegroups.com
One more thing, 
You mentioned that you created an edge collection that connects some objects. After that you continue saying that you query by example. You are also using the provided graph functionality in AQL, right? 

Muumimojo

unread,
Mar 4, 2013, 12:23:26 PM3/4/13
to aran...@googlegroups.com
We are trying to get an Array with the edges from a specific user, we thought the method 'edges' should work but it gives us all the edges and not only the ones from the user we defined, we are not completelly sure in that method what they mean by '$vertexHandle' so maybe the error is there.

 $cursorClient= $handler->getByExample('users', array('id'=>$_SESSION['users_id']));
    $user= $cursorClient->current();

    $user_products=array();
    $user_products=$edgeDocumentHandler->edges("recommendation", $user->getHandle(), "out");



After we get the array with the edges we are planning to use the attribute '_to' to see if any of the products in the cart already exist in the edges array, but we didn't find a way to access this attribute (_to) or any other attribute from the edges.

foreach ($_SESSION['cart'] as $pid => $value) { $cursorProducts= $handler->getByExample('products', array('id'=>$pid)); $product= $cursorProducts->current(); $recommendation_id = substr($user_products['documents'][0], 15); for ($i=0;$i<count($user_products['documents']);$i++) { //Compare the attribute _to from the edges in $user_products["documents"] 
//with the ID of the products in the cart
}
}


Message has been deleted

Frank Mayer

unread,
Mar 4, 2013, 1:29:11 PM3/4/13
to aran...@googlegroups.com
Hi,

edges()  returns a cursor, which you have to iterate. The value that is extracted is your document.

foreach ($cursor as $key=>$value){
$to = $value->_to; should give you your _to.
}

If I may give another hint on the design: I would not keep the cart in a session only but have it in the database. Most shop software out there handle a cart or an order in the same table/document, for example `order`. Once the user has checked out the cart it becomes an order and some flag is set to signalize that.
If you do it like that, you can have ArangoDB do all the lookups for you.



and this: http://www.arangodb.org/manuals/current/Aql.html   (look for traversal)

Hope that helps.

Frank

Muumimojo

unread,
Mar 5, 2013, 2:29:23 PM3/5/13
to aran...@googlegroups.com
Hi, we are still having some troubles with the edges, we have and edge collection called 'recommendation' and we were trying to get the edges from there that has a specific user as a vertex. We use the method edges and store the result in $user_products but after we try to extract some data from each of the edges inside we get an error
Fatal error: Call to a member function getFrom() on a non-object in C:\xampp\htdocs\onlineshop\checkout.php on line 83

We also tryed to do print the data using print $value->_from; instead, but it didn't print anything

$cursorClient= $handler->getByExample('users', array('id'=>$_SESSION['users_id']));
$user= $cursorClient->current();

$user_products=$edgeDocumentHandler->edges("recommendation", $user->getHandle(),"out");

foreach ($user_products['documents'] as $key =>$value){
         print $value->getFrom();
}

Frank Mayer

unread,
Mar 5, 2013, 3:08:21 PM3/5/13
to aran...@googlegroups.com
Hi,
you should not use ['documents'] in that foreach. Your cursor which you must iterate is: $user_products, not $user_products['documents'].

Please take a look at the CollectionExtendedTest.php and DocumentExtendedTest.php. Cursors are used there in foreach loops to iterate over them and store their values in arrays for the tests. After you have extracted the documentobjects out of the cursors you will be able to do any get() methods like getFrom() ... etc...


You can also always do $user_products->getAll() in order to create an array of all returned documents and iterate through this if you are more comfortable with that. However getAll() fetches all the documents before returning the array in contrast to the iteration of the cursor, which only fetches documents as needed (you can theoretically break out of the loop earlier if you like ).

Please consult the documentation in the docs folder for the commands related to cursor.

Regards,
Frank

Muumimojo

unread,
Mar 7, 2013, 10:59:52 AM3/7/13
to aran...@googlegroups.com
Sorry but we are still having the same problem, we removed ['documents'] from the code

foreach ($user_products as $key =>$value){
         $resultingDocument[$key] = $value;
    }
    print $resultingDocument[0]->getFrom(); //line 93


and the following error is shown

Notice: Undefined offset: 0 in C:\xampp\htdocs\onlineshop\checkout.php on line 93

Fatal error: Call to a member function getFrom() on a non-object in C:\xampp\htdocs\onlineshop\checkout.php on line 93

Beside i tryed to use the getAll() in the following way

print_r($alluser_products->getAll()); //line 92

but i get the error
Fatal error: Call to a member function getAll() on a non-object in C:\xampp\htdocs\onlineshop\checkout.php on line 92

I'm not completelly sure but for me seems like what i get from edges is a matrix with 1 column called 'documents' and many rows that contain strings instead of cursors

Frank Mayer

unread,
Mar 7, 2013, 7:21:16 PM3/7/13
to aran...@googlegroups.com
Hi,
Could you please post the result of var_dump($user_products)? 
and then a var_dump(resultingDocument) of the first two $resultingdocuments in the forEach loop?
These Var_dumps can be quiet large, so if you'd put them in a gist and post the link in the next message , it would be great.

Muumimojo

unread,
Mar 8, 2013, 10:56:38 AM3/8/13
to aran...@googlegroups.com
var_dump.rtf

Frank Mayer

unread,
Mar 8, 2013, 11:14:37 AM3/8/13
to aran...@googlegroups.com
Of course... it's the location for you to fetch the document... not the actual document itself... Sorry I didn't see that earlier.

If you check the documentation for Edgehandler->edges it says that it returns an array of cursors ... in other words the location of the document for you to fetch.

Try this: 
 
$myEdgeHandler=new EdgeHandler($connection);

foreach ($user_products as $key =>$value){
$resultingDocument[$key] = $myEdgeHandler->get(getCollectionIdFromLocation($value),getDocumentIdFromLocation($value));

}
print $resultingDocument[0]->getFrom();



On Friday, March 8, 2013 5:56:38 PM UTC+2, Muumimojo wrote:

Reply all
Reply to author
Forward
0 new messages