-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserRepository.php
97 lines (90 loc) · 2.85 KB
/
UserRepository.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
class UserRepository
{
private $em;
public function __construct()
{
$this->em = getEntityManager();
}
/************************************
* LSD
************************************/
public function load($byId = null)
{
$qb = $this->em->createQueryBuilder();
$qb->select('u')
->from('user', 'u')
->where('1=1');
if (!is_null($byId)) {
$qb->andWhere('u.id = :id')
->setParameter('id', $byId);
}
return $qb->getQuery()->getResult();
}
public function save($login, $password, $name, $lastName, $email, $status, $id = null)
{
if (is_null($id)) {
try {
$user = new User();
$user->setLogin($login);
$user->setPassword($password);
$user->setName($name);
$user->setLastname($lastName);
$user->setEmail($email);
$user->setStatus($status);
$this->em->persist($user);
$this->em->flush();
$this->em->close();
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
} else {
$qb = $this->em->createQueryBuilder();
$qb->update('user', 'u');
if (!is_null($login)) {
$qb->set('u.login', ':login')
->setParameter(':login', $login);
}
if (!is_null($password)) {
$qb->set('u.password', ':password')
->setParameter(':password', $password);
}
if (!is_null($name)) {
$qb->set('u.name', ':name')
->setParameter(':name', $name);
}
if (!is_null($lastName)) {
$qb->set('u.lastName', ':lastName')
->setParameter(':lastName', $lastName);
}
if (!is_null($email)) {
$qb->set('u.email', ':email')
->setParameter(':email', $email);
}
if (!is_null($status)) {
$qb->set('u.status', ':status')
->setParameter(':status', $status);
}
$qb->where('u.id = :id')
->setParameter('id', $id);
$qb->getQuery()->getResult();
}
return true;
}
public function delete($id)
{
$qb = $this->em->createQueryBuilder();
try {
$qb->delete('user', 'u')
->where('u.id = :id')
->setParameter('id', $id)
->getQuery()->getResult();
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
return true;
}
/************************************
* OTHER
************************************/
}