Skip to content

Commit 46ffd2c

Browse files
Khienfacebook-github-bot
Khien
authored andcommitted
Get Next and Previous should not use cursors (#623)
Summary: https://developers.facebook.com/docs/graph-api/results/ - next : The Graph API endpoint that will return the next page of data. If not included, this is the last page of data. Due to how pagination works with visibility and privacy, it is possible that a page may be empty but contain a next paging link. Stop paging when the next link no longer appears. - previous : The Graph API endpoint that will return the previous page of data. If not included, this is the first page of data. === For example: If my cursor has total 4 items (default limit is 25) ``` $cursor->setDefaultUseImplicitFetch(true); foreach ($cursor as $item) { $data[] = $item->exportAllData(); } ``` So I need to make one request to get the cursor with 4 items. The current code **getNext()** will always return a URL and ``` public function fetchAfter() { $request = $this->createAfterRequest(); if (!$request) { return; } $this->appendResponse($request->execute()); } ``` **fetchAfter()** will **execute** one more request and return an empty data. So, we need to make 2 requests to get for only 4 items. So if **$content['paging']['next']** is empty, we should return null and do not need to make more requests Pull Request resolved: #623 Reviewed By: satwikareddy3 Differential Revision: D67995304 Pulled By: stcheng fbshipit-source-id: 78e3cc4167e10f228e7a3c4e572c39b87634c602
1 parent 8193ecb commit 46ffd2c

File tree

2 files changed

+2
-15
lines changed

2 files changed

+2
-15
lines changed

src/FacebookAds/Cursor.php

-14
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,6 @@ public function getPrevious() {
274274
return $content['paging']['previous'];
275275
}
276276

277-
$before = $this->getBefore();
278-
if ($before !== null) {
279-
$request = $this->createUndirectionalizedRequest();
280-
$request->getQueryParams()->offsetSet('before', $before);
281-
return $request->getUrl();
282-
}
283-
284277
return null;
285278
}
286279

@@ -293,13 +286,6 @@ public function getNext() {
293286
return $content['paging']['next'];
294287
}
295288

296-
$after = $this->getAfter();
297-
if ($after !== null) {
298-
$request = $this->createUndirectionalizedRequest();
299-
$request->getQueryParams()->offsetSet('after', $after);
300-
return $request->getUrl();
301-
}
302-
303289
return null;
304290
}
305291

test/FacebookAdsTest/CursorTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ protected function createSampleResponseContent() {
6666
'after' => $this->getUniquePageId(),
6767
'before' => $this->getUniquePageId(),
6868
),
69+
'next' => $this->createUnparameterizedUrl() . '?after=' . $this->getUniquePageId(),
70+
'previous' => $this->createUnparameterizedUrl() . '?before=' . $this->getUniquePageId(),
6971
),
7072
) + $this->createEmptyResponseContent();
7173

@@ -339,7 +341,6 @@ public function testImplicitFetch() {
339341
$response = $this->createResponseChainMock(3);
340342
$cursor = new Cursor($response, $this->objectPrototype);
341343

342-
$count = 0;
343344
while ($cursor->valid()) {
344345
$cursor->prev();
345346
}

0 commit comments

Comments
 (0)