Skip to content

Commit 89901e4

Browse files
committed
Refactor pagination in Profiles and Users Controllers
1 parent e486e79 commit 89901e4

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

src/Controllers/ProfilesController.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,9 @@ public function indexAction(): void
4646
*/
4747
public function searchAction()
4848
{
49-
$numberPage = 1;
5049
if ($this->request->isPost()) {
51-
$query = Criteria::fromInput($this->di, 'Vokuro\Models\Profiles', $this->request->getPost());
50+
$query = Criteria::fromInput($this->di, 'Vokuro\Models\Profiles', $this->request->getPost());
5251
$this->persistent->searchParams = $query->getParams();
53-
} else {
54-
$numberPage = $this->request->getQuery("page", "int");
5552
}
5653

5754
$parameters = [];
@@ -69,9 +66,10 @@ public function searchAction()
6966
}
7067

7168
$paginator = new Paginator([
72-
"data" => $profiles,
73-
"limit" => 10,
74-
"page" => $numberPage,
69+
'model' => Profiles::class,
70+
'parameters' => $parameters,
71+
'limit' => 10,
72+
'page' => $this->request->getQuery('page', 'int', 1),
7573
]);
7674

7775
$this->view->setVar('page', $paginator->paginate());

src/Controllers/UsersController.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace Vokuro\Controllers;
1414

1515
use Phalcon\Mvc\Model\Criteria;
16-
use Phalcon\Paginator\Adapter\Model as Paginator;
16+
use Phalcon\Paginator\Adapter\QueryBuilder as Paginator;
1717
use Vokuro\Forms\ChangePasswordForm;
1818
use Vokuro\Forms\UsersForm;
1919
use Vokuro\Models\PasswordChanges;
@@ -41,23 +41,24 @@ public function indexAction(): void
4141
/**
4242
* Searches for users
4343
*/
44-
public function searchAction()
44+
public function searchAction(): void
4545
{
46-
$numberPage = $this->request->getQuery('page', 'int', 1);
47-
$parameters = Criteria::fromInput($this->di, Users::class, $this->request->getQuery())->getParams();
46+
$builder = Criteria::fromInput($this->di, Users::class, $this->request->getQuery());
4847

49-
$users = Users::find($parameters);
50-
if (count($users) === 0) {
48+
$count = Users::count($builder->getParams());
49+
if ($count === 0) {
5150
$this->flash->notice('The search did not find any users');
52-
return $this->dispatcher->forward([
51+
$this->dispatcher->forward([
5352
'action' => 'index',
5453
]);
54+
55+
return;
5556
}
5657

5758
$paginator = new Paginator([
58-
'data' => $users,
59+
'builder' => $builder->createBuilder(),
5960
'limit' => 10,
60-
'page' => $numberPage,
61+
'page' => $this->request->getQuery('page', 'int', 1),
6162
]);
6263

6364
$this->view->setVar('page', $paginator->paginate());

src/Models/Users.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Users extends Model
6969

7070
public function initialize()
7171
{
72-
$this->belongsTo('profilesId', Profiles::class, 'id', [
72+
$this->hasOne('profilesId', Profiles::class, 'id', [
7373
'alias' => 'profile',
7474
'reusable' => true,
7575
]);

0 commit comments

Comments
 (0)