danielmewes / php-rql

A PHP client driver for the RethinkDB query language (ReQL).
http://php-rql.dnsalias.net/
339 stars 60 forks source link

Select all documents in a table #97

Closed ryanaverill closed 9 years ago

ryanaverill commented 9 years ago

I'm running the last version of php-rql but when I run something like this:

$data = r\table($collection)->run($conn); var_dump($data);

I'm expecting something like rethinkdb returns:

{ "firstName": "first" , "id": "5d5ccdc7-3e6d-4c1d-a564-d2fa87d46b51" , "lastName": "last" }

But instead I get something like this. I can clean this up myself but is this expected, any chance there is an output option to clean up this extra verbose info? Hopefully that makes sense

object(r\Cursor)#31 (9) { ["token":"r\Cursor":private]=> int(263484269) ["connection":"r\Cursor":private]=> object(r\Connection)#26 (7) { ["socket":"r\Connection":private]=> resource(5) of type (stream) ["host":"r\Connection":private]=> string(14) "192.168.33.149" ["port":"r\Connection":private]=> int(28015) ["defaultDb":"r\Connection":private]=> object(r\Db)#21 (3) { ["positionalArgs":"r\Query":private]=> array(1) { [0]=> object(r\StringDatum)#22 (4) { ["value":"r\Datum":private]=> string(4) "test"

danielmewes commented 9 years ago

@ryanaverill the returned object is a cursor. Since a table can be much too large to fit into memory on the client, RethinkDB returns it in chunks rather than all at once. A cursor can be iterated over, and more chunks are requested from the server as needed while doing that.

There are three options to get all data:

  1. Iterating over the cursor and handling the data one document at a time:

    $cursor = r\table($collection)->run($conn);
    foreach ($cursor as $doc) {
    var_dump($doc);
    }
  2. Convert the cursor into an array on the client side (make sure the table is small, or you might run out of memory):

    $cursor = r\table($collection)->run($conn);
    $allData = $cursor->toArray();
    var_dump($allData); // $allData is now an array of all the documents

    This uses the toArray method http://danielmewes.dnsalias.net/~daniel/php-rql-api/#Cursors

  3. Convert the table into an array on the server side (this is limited to 100,000 elements by default, and can use large amounts of memory both on the server and the client if the table is large):

    $allData = r\table($collection)->coerceTo("ARRAY")->run($conn);
    var_dump($allData);

    This uses the coerceTo ReQL command http://danielmewes.dnsalias.net/~daniel/php-rql-api/#Control+structures

ryanaverill commented 9 years ago

Awesome, thank you so much!

Sent from my iPhone

On Jul 19, 2015, at 12:05 AM, Daniel Mewes notifications@github.com wrote:

@ryanaverill the returned object is a cursor. Since a table can be much too large to fit into memory on the client, RethinkDB returns it in chunks rather than all at once. A cursor can be iterated over, and more chunks are requested from the server as needed while doing that.

There are three options to get all data:

  1. Iterating over the cursor and handling the data one document at a time:

$cursor = r\table($collection)->run($conn); foreach ($doc in $cursor) { var_dump($doc); } Convert the cursor into an array on the client side (make sure the table is small, or you might run out of memory): $cursor = r\table($collection)->run($conn); $allData = $cursor->toArray(); var_dump($allData); // $allData is now an array of all the documents This uses the toArray method http://danielmewes.dnsalias.net/~daniel/php-rql-api/#Cursors

  1. Convert the table into an array on the server side (this is limited to 100,000 elements by default, and can use large amounts of memory both on the server and the client if the table is large):

$allData = r\table($collection)->coerceTo("ARRAY")->run($conn); var_dump($allData); This uses the coerceTo ReQL command http://danielmewes.dnsalias.net/~daniel/php-rql-api/#Control+structures

— Reply to this email directly or view it on GitHub.