0

In web use of firebase, I can use "value" and "key" for the "startAt" method, as demonstrated here:

https://stackoverflow.com/a/38024909/3466502

And documented here:

https://firebase.google.com/docs/reference/js/firebase.database.Reference?hl=pt-br#startAt

But I need this feature in a REST consume and I'm not finding any reference how I should do this.

Does anyone know how to do this?

I am using the PHP packages recommended by Firebase, but in their documentation I have not been able to get information on how I could do this ... :(

I have a data structure like this:

"list": {
    "-KsaZNyjd91tEAKjDffA": {"name": "a", "age" => 20},
    "-KsaZNynhTFuDBLpdmKv": {"name": "b", "age" => 21},
    "-KsaZNyoAoYAfHl-f6KF": {"name": "b", "age" => 22},
    "-KsaZiL4HJcoCYksHBEn": {"name": "b", "age" => 23},
    ...
}

I need to return two items per page dynamically. In JavaScript it was easy doing so:

var list = firebase.database().ref("list").orderByChild("name").limitToFirst(2);

var page_1 = $firebaseArray(list);
var page_2 = $firebaseArray(list.startAt('b', '-KsaZNyoAoYAfHl-f6KF'));

Using REST I created the following code:

$list = $db->getReference('list')->orderByChild('name')->limitToFirst(2);

$page_1 = $list->getValue();
$page_2 = $list->startAt('b', '-KsaZNyoAoYAfHl-f6KF')->getValue();
//                                      /
// "key" does not work in this method--/

But the "startAt" method of the package does not allow me to determine the "key" as the second parameter and thus the content of the second page is not what is expected.

Page 1

{"name": "a", "age" => 20},
{"name": "b", "age" => 21}

Page 2

{"name": "b", "age" => 21}, // <--- This is the last one on page one
{"name": "b", "age" => 22}  // <--- This should be the first on page 2

I looked at the PHP package method and it did not really implement anything for the "key". https://firebase-php.readthedocs.io/en/latest/realtime-database.html#startat

public function startAt($value): Query
    {
        return $this->withAddedFilter(new Filter\StartAt($value));
    }

So I looked at the firebase documentation for REST requests, but I also could not identify which parameter I should specify the "key" of my query. https://firebase.google.com/docs/database/rest/retrieve-data#consultas-de-intervalo

Alex Oliveira
  • 133
  • 1
  • 9
  • The REST API supports the same operators: `startAt`, `endAt`, etc. They're documented here: https://firebase.google.com/docs/database/rest/retrieve-data#section-complex-queries. If you're having trouble making them work, show the [minimal code that reproduces where you are stuck](http://stackoverflow.com/help/mcve). – Frank van Puffelen Aug 28 '17 at 02:06
  • Thank you @FrankVanPuffelen. I am reading this documentation page but still can not find the solution to my problem. I added more information to try to better explain where I was stuck. – Alex Oliveira Aug 28 '17 at 04:32

1 Answers1

0

If you look at the code for startAt, it looks like that PHP library doesn't implement the second parameter.

Your best bet is to file an issue on the Github repo asking to support the second parameter to startAt() (and endAt()), or to change the code yourself and file a Pull Request.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I need a slightly faster solution at this point so I thought I'd implement this feature myself. But, I am reviewing the firebase documentation and I can not figure out which parameter I should use to inform my "key" in a REST request. I found several examples of usage for "startAt", "endAt", "equalTo" and others, but none along with the "key" – Alex Oliveira Aug 28 '17 at 22:19
  • A few relevant answers: https://stackoverflow.com/questions/41278544/how-to-paging-query-from-firebase-using-android-firebaseui/41285470#41285470, https://stackoverflow.com/questions/43376331/firebase-query-order-by-and-start-at-name/43376894#43376894, https://stackoverflow.com/questions/31995881/firebase-orderbychild-with-startats-second-argument-w-pagination-not-odering – Frank van Puffelen Aug 28 '17 at 23:43
  • It seems like all this time I'm looking for something that does not exist. It seems that api REST does not allow the use of "startAt" with a "key". Https://stackoverflow.com/a/40514418/3466502 – Alex Oliveira Aug 30 '17 at 02:43
  • Aargh... that makes sense. Sorry for not thinking about that. – Frank van Puffelen Aug 30 '17 at 02:58