forked from ovr/phalcon-module-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsersController.php
81 lines (69 loc) · 1.84 KB
/
UsersController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Api\Controller;
use Exception;
use Phalcon\Mvc\Controller;
use Phalcon\Paginator\Adapter\QueryBuilder;
use User\Model\User;
/**
* Class UsersController
* @package Api\Controller
*/
class UsersController extends BaseController
{
public function indexAction()
{
$limit = $this->getQueryLimit();
$page = $this->getQueryPage();
$builder = $this->modelsManager->createBuilder()
->from('User\Model\User')
->orderBy('id ASC');
$paginator = new QueryBuilder(array(
"builder" => $builder,
"limit"=> $limit,
"page" => $page
));
$result = array();
$page = $paginator->getPaginate();
/**
* @var $user User
*/
foreach ($page->items as $user) {
$result[] = [
'id' => $user->id,
'nick' => $user->nick,
'firstname' => $user->firstname,
'lastname' => $user->lastname
];
}
return array(
'success' => true,
'result' => array(
'users' => $result,
'pages' => $page->total_pages,
'total' => $page->total_items
)
);
}
public function getAction($id)
{
if ($id <= 0) {
throw new Exception('Wrong id passed', 500);
}
/**
* @var $user User|boolean
*/
$user = User::findFirst($id);
if (!$user) {
throw new Exception('User not found', 404);
}
return array(
'success' => true,
'result' => array(
'id' => $user->id,
'nick' => $user->nick,
'firstname' => $user->firstname,
'lastname' => $user->lastname
)
);
}
}