mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-21 00:46:27 +02:00
Add strategy for more efficient pagination in API listing of entries by using before_id and after_id wheres instead of pagination if possible
This commit is contained in:
parent
04b0f1267a
commit
0e11bcda17
10 changed files with 156 additions and 28 deletions
|
@ -20,10 +20,12 @@ namespace models;
|
|||
* @param int $id_user : user id
|
||||
* @param ?int $limit : Number of entry to return or null
|
||||
* @param ?int $offset : Number of entry to ignore or null
|
||||
* @param ?int $after_id : If provided use where id > $after_id instead of offset
|
||||
* @param ?int $before_id : If provided use where id < $before_id instead of offset
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function list_for_user(int $id_user, $limit, $offset)
|
||||
public function list_for_user(int $id_user, $limit, $offset, ?int $after_id = null, ?int $before_id = null)
|
||||
{
|
||||
$query = '
|
||||
SELECT pg.*, COUNT(pgp.id) as nb_phone
|
||||
|
@ -31,9 +33,43 @@ namespace models;
|
|||
LEFT JOIN phone_group_phone as pgp
|
||||
ON pg.id = pgp.id_phone_group
|
||||
WHERE pg.id_user = :id_user
|
||||
GROUP BY pg.id
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
if ($after_id || $before_id)
|
||||
{
|
||||
$offset = null;
|
||||
}
|
||||
|
||||
if ($after_id)
|
||||
{
|
||||
$query .= '
|
||||
AND pg.id > :after_id
|
||||
GROUP BY pg.id
|
||||
ORDER BY g.id
|
||||
';
|
||||
$params['after_id'] = $after_id;
|
||||
}
|
||||
elseif ($before_id)
|
||||
{
|
||||
$query .= '
|
||||
AND pg.id < :before_id
|
||||
GROUP BY pg.id
|
||||
ORDER BY g.id DESC
|
||||
';
|
||||
$params['before_'] = $before_id;
|
||||
}
|
||||
else
|
||||
{
|
||||
$query .= '
|
||||
GROUP BY pg.id
|
||||
ORDER BY pg.id
|
||||
';
|
||||
}
|
||||
|
||||
if (null !== $limit)
|
||||
{
|
||||
$limit = (int) $limit;
|
||||
|
@ -46,9 +82,10 @@ namespace models;
|
|||
}
|
||||
}
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
if ($before_id)
|
||||
{
|
||||
return array_reverse($this->_run_query($query, $params));
|
||||
}
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue