Skip to content

Commit

Permalink
fix paginate toArray bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sakuraovq committed Aug 1, 2019
1 parent 6bccebb commit 3027287
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 102 deletions.
84 changes: 84 additions & 0 deletions src/db/src/Concern/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,88 @@ public function unless($value, $callback, $default = null)

return $this;
}


/**
* Paginate the given query into a simple paginator.
*
* @param int $page
* @param int $perPage
* @param array $columns
*
* @return array
*/
public function paginate(int $page = 1, int $perPage = 15, array $columns = ['*']): array
{
$list = [];
// Run a pagination count query
if ($count = $this->getCountForPagination()) {
// Get paginate records
$list = $this->forPage($page, $perPage)->get($columns)->toArray();
}

return [
'count' => $count,
'list' => $list,
'page' => $page,
'perPage' => $perPage,
'pageCount' => (int)ceil($count / $perPage)
];
}

/**
* Paginate the given query into a simple paginator.
*
* @param int $perPage
* @param int $lastId
* @param array $columns
* @param string $primary
*
* @return array
*/
public function paginateAfterId(
int $perPage = 15,
int $lastId = 0,
array $columns = ['*'],
string $primary = null
): array {
$list = [];
$thisLastId = 0;

if ($primary === null) {
if ($this instanceof Model) {
// If primary is null default user primary column name
$primary = is_null($primary) ? $this->getModel()->getKeyName() : $primary;
} else {
$primary = 'id';
}
}

// Run a pagination count query
if ($count = $this->getCountForPagination()) {
// Auto Join primary field
if ($columns !== ['*'] && in_array($primary, $columns, true) === false) {
$columns[] = $primary;
}

// Get paginate records
$list = $this->forPageAfterId($perPage, $lastId ?: null, $primary)->get($columns)->toArray();

// Alias parse
if (strpos($primary, '.') !== false) {
$primaryAlias = explode('.', $primary);
$primary = end($primaryAlias);
}
$thisLastId = end($list)[$primary] ?? 0;
}

return [
'count' => $count,
'list' => $list,
'lastId' => $thisLastId,
'perPage' => $perPage,
'pageCount' => (int)ceil($count / $perPage)
];
}

}
24 changes: 1 addition & 23 deletions src/db/src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
* @method float|int average(string $column)
* @method Connection getConnection()
* @method string implode(string $column, string $glue = '')
* @method array paginate(int $page = 1, int $perPage = 15, array $columns = ['*'])
* @method Builder useWritePdo()
*
*/
Expand Down Expand Up @@ -163,7 +162,6 @@ class Builder
'implode',
'pluck',
'getConnection',
'paginate',
'join',
'joinSub',
'joinWhere',
Expand All @@ -180,6 +178,7 @@ class Builder
'fromSub',
'selectRaw',
'truncate',
'getCountForPagination',
];

/**
Expand Down Expand Up @@ -749,27 +748,6 @@ public function forPageAfterId(int $perPage = 15, int $lastId = null, string $co
return $this;
}


/**
* Paginate the given query into a simple paginator.
*
* @param int $perPage
* @param int $lastId
* @param array $columns
* @param string|null $primary
*
* @return array
* @throws ContainerException
* @throws DbException
* @throws ReflectionException
*/
public function paginateAfterId(int $perPage = 15, int $lastId = 0, array $columns = ['*'], string $primary = null): array {
// If primary is null default user primary column name
$primary = is_null($primary) ? $this->getModel()->getKeyName() : $primary;

return $this->query->paginateAfterId($perPage, $lastId, $columns, $primary);
}

/**
* Save a new model and return the instance.
*
Expand Down
1 change: 1 addition & 0 deletions src/db/src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
* @method static float|int average(string $column)
* @method static void truncate()
* @method static Builder useWritePdo()
* @method static int getCountForPagination(array $columns = ['*'])
*/
abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
{
Expand Down
76 changes: 0 additions & 76 deletions src/db/src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2395,82 +2395,6 @@ protected function runSelect(): array
return $this->getConnection()->select($this->toSql(), $this->getBindings(), !$this->useWritePdo);
}

/**
* Paginate the given query into a simple paginator.
*
* @param int $page
* @param int $perPage
* @param array $columns
*
* @return array
*/
public function paginate(int $page = 1, int $perPage = 15, array $columns = ['*']): array
{
$list = [];
// Run a pagination count query
if ($count = $this->getCountForPagination()) {
// Get paginate records
$list = $this->forPage($page, $perPage)->get($columns)->toArray();
}

return [
'count' => $count,
'list' => $list,
'page' => $page,
'perPage' => $perPage,
'pageCount' => ceil($count / $perPage)
];
}

/**
* Paginate the given query into a simple paginator.
*
* @param int $perPage
* @param int $lastId
* @param array $columns
* @param string $primary
*
* @return array
* @throws ContainerException
* @throws DbException
* @throws ReflectionException
*/
public function paginateAfterId(
int $perPage = 15,
int $lastId = 0,
array $columns = ['*'],
string $primary = 'id'
): array {
$list = [];
$thisLastId = 0;

// Run a pagination count query
if ($count = $this->getCountForPagination()) {
// Auto Join primary field
if ($columns !== ['*'] && in_array($primary, $columns, true) === false) {
$columns[] = $primary;
}

// Get paginate records
$list = $this->forPageAfterId($perPage, $lastId ?: null, $primary)->get($columns)->toArray();

// Alias parse
if (strpos($primary, '.') !== false) {
$primaryAlias = explode('.', $primary);
$primary = end($primaryAlias);
}
$thisLastId = end($list)[$primary] ?? 0;
}

return [
'count' => $count,
'list' => $list,
'lastId' => $thisLastId,
'perPage' => $perPage,
'pageCount' => ceil($count / $perPage)
];
}

/**
* Get the count of the total records for the paginator.
*
Expand Down
13 changes: 10 additions & 3 deletions src/db/test/unit/Eloquent/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,18 +413,25 @@ public function testPaginate()
{
$perPage = 2;
$page = 1;
$res = User::paginate($page, $perPage, ['name', 'password', 'id']);
$res = User::paginate($page, $perPage, ['id', 'name', 'password', 'user_desc']);
$res1 = User::select('id')
->where('id', '>', 0)
->addSelect(['name', 'password'])
->addSelect(['name', 'password', 'user_desc'])
->paginate($page, $perPage);


$afterResult = User::where('name', '!=', '')
->from('user as u')
->leftJoin('count as c', 'u.id', '=', 'c.user_id')
->paginateAfterId($perPage, 1, ['name', 'password'], 'u.id');

$this->assertEquals($res['perPage'], $afterResult);
$afterResult1 = User::where('name', '!=', '')
->from('user as u')
->paginateAfterId($perPage, 1, ['name', 'password', 'user_desc']);

$this->assertEquals($res['perPage'], $afterResult1['perPage']);
$this->assertEquals($res['perPage'], $afterResult['perPage']);

$this->assertEquals($res, $res1);
$this->assertIsArray($res);
$this->assertArrayHasKey('list', $res);
Expand Down

0 comments on commit 3027287

Please sign in to comment.