odata-client-php icon indicating copy to clipboard operation
odata-client-php copied to clipboard

Question about @odata.nextLink

Open drblitz-weblab opened this issue 7 years ago • 6 comments
trafficstars

For example we have request like this

$people = $odataClient->from('People')->take(2)->get();

How I can get access to @odata.nextLink

drblitz-weblab avatar Oct 26 '18 22:10 drblitz-weblab

@t3Golden Did you figure this out?

stefantalen avatar Jul 28 '20 09:07 stefantalen

As best I can tell, ODataRequest::execute() calls ODataResponse::getResponseAsObject() which extracts the value array from the returned json object and discards everything else. Eventually that array gets passed to the Illuminate Collection class, so I'm not even sure how one would go about addressing the loss of useful metadata without a breaking change.

jbramleycl avatar Aug 21 '20 14:08 jbramleycl

@jbramleycl that is correct. However, we're looking at returning a custom collection or LengthAwarePaginator that is still an iterator to keep backward compatibility while enabling you to page through multi-page results.

anderly avatar Aug 21 '20 17:08 anderly

Any process on this yet? Currently, I am doing a count() first, and then I page through the results doing a skip which is a nasty workaround since OData is providing the next page. Would be really great to have the @odata.nextLink property available somehow.

Cabag avatar Dec 02 '20 14:12 Cabag

:+1: I just ran into that problem as well. This issue is open for 4 years now. Is there a solution now? Otherwise I will build an own implementation.

mschop avatar Aug 02 '22 05:08 mschop

i. need. this.

working with an api that enforces pagination. can you provide your wip so we can take it from there, please?

glaszig avatar Aug 13 '22 22:08 glaszig

This is now supported. Setting $odataClient->setEntityReturnType(false) will allow you to get the full ODataResponse object, from which you can then call getSkipToken(),

anderly avatar Sep 07 '23 06:09 anderly

@glaszig, @mschop, @Cabag, @jbramleycl, @stefantalen, @drblitz-weblab,

Sorry for the delay on this.

Please take a look at PR142 and feel free to pull down the source branch feature/paging-with-lazycollection which adds support for native paging over the paginated OData response using LazyCollection via new cursor methods on the Builder and ODataClient.

Using the new cursor method on the Builder, you can page through the paginated response like so:

    // Request 8 records per page (library default is 25)
    $odataClient = new ODataClient('https://services.odata.org/V4/TripPinService', function($request) {
        $request->headers[RequestHeader::PREFER] = Constants::ODATA_MAX_PAGE_SIZE . '=' . 8;
    })

    // Or, using the new 'pageSize' method on the Builder
    $data = $odataClient->from('People')->pageSize(8)->cursor();

    $totalRecordCount = $data->count(); // Will return 20, which is the total number of 'Person' records in the Person EntitySet

    $firstPerson = $data->first(); // Will return the first person in the EntitySet (Russell Whyte)
    $lastPerson = $data->last(); // Will return the last person in the EntitySet (Krista Kemp, from page 3 of the paginated results)

    // Skip the first 8 records (page 1) and get the first person on page 2 (the 9th Person)
    // This will issue a request to the second page in order to obtain the 9th person in the EntitySet (Marshall Garay)
    $ninthPerson = $data->skip(8)->first();

Still doing some additional review and testing, but wanted to get some feedback before merging.

anderly avatar Sep 07 '23 20:09 anderly

Updated wiki with paging examples here.

anderly avatar Sep 08 '23 16:09 anderly