|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: siim |
|
5
|
|
|
* Date: 21.01.19 |
|
6
|
|
|
* Time: 8:26 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Sf4\Api\Repository; |
|
10
|
|
|
|
|
11
|
|
|
use Doctrine\DBAL\DBALException; |
|
12
|
|
|
use Doctrine\DBAL\Driver\Statement; |
|
13
|
|
|
use Doctrine\ORM\EntityRepository; |
|
14
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
|
15
|
|
|
use Doctrine\ORM\NoResultException; |
|
16
|
|
|
use Doctrine\ORM\Query; |
|
17
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
18
|
|
|
use Sf4\Api\Exception\InvalidObjectTypeException; |
|
19
|
|
|
|
|
20
|
|
|
abstract class AbstractRepository extends EntityRepository |
|
21
|
|
|
{ |
|
22
|
|
|
const TABLE_NAME = null; |
|
23
|
|
|
|
|
24
|
|
|
const FIELD_ID = 'main.id'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param $id |
|
28
|
|
|
* @return array|null |
|
29
|
|
|
* @throws \Exception |
|
30
|
|
|
*/ |
|
31
|
|
|
public function findDataById($id): ?array |
|
32
|
|
|
{ |
|
33
|
|
|
$this->throwExceptionWhenDbNameIsNull(); |
|
34
|
|
|
$qb = $this->createQueryBuilder('main'); |
|
35
|
|
|
$qb->where( |
|
36
|
|
|
$qb->expr()->eq('main.id', ':id') |
|
37
|
|
|
); |
|
38
|
|
|
$qb->setParameter(':id', $id); |
|
39
|
|
|
|
|
40
|
|
|
$results = $qb->getQuery()->getResult(Query::HYDRATE_ARRAY); |
|
41
|
|
|
if ($results) { |
|
42
|
|
|
return $results[0]; |
|
43
|
|
|
} |
|
44
|
|
|
return null; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @throws \Exception |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function throwExceptionWhenDbNameIsNull() |
|
51
|
|
|
{ |
|
52
|
|
|
if (static::TABLE_NAME === null) { |
|
|
|
|
|
|
53
|
|
|
throw new InvalidObjectTypeException('Invalid repository'); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $sql |
|
59
|
|
|
* @param array $params |
|
60
|
|
|
* @return array|null |
|
61
|
|
|
*/ |
|
62
|
|
|
public function findOneOrNullData(string $sql, array $params = []): ?array |
|
63
|
|
|
{ |
|
64
|
|
|
$statement = $this->getStatement($sql, $params); |
|
65
|
|
|
if ($statement) { |
|
|
|
|
|
|
66
|
|
|
$data = $statement->fetch(); |
|
67
|
|
|
if ($data !== false) { |
|
68
|
|
|
return $data; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $sql |
|
77
|
|
|
* @param array $params |
|
78
|
|
|
* @return Statement|null |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function getStatement(string $sql, array $params = []): ?Statement |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
|
|
$params = array_values($params); |
|
84
|
|
|
$connection = $this->getEntityManager()->getConnection(); |
|
85
|
|
|
$statement = $connection->prepare($sql); |
|
86
|
|
|
$statement->execute($params); |
|
87
|
|
|
return $statement; |
|
88
|
|
|
} catch (DBALException $e) { |
|
89
|
|
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function findDataResults(string $sql, array $params = []): array |
|
94
|
|
|
{ |
|
95
|
|
|
$statement = $this->getStatement($sql, $params); |
|
96
|
|
|
if ($statement) { |
|
|
|
|
|
|
97
|
|
|
return $statement->fetchAll(); |
|
98
|
|
|
} |
|
99
|
|
|
return []; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $id |
|
104
|
|
|
* @return mixed|null |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getEntityById(string $id) |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->getEntityBy('id', $id); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $field |
|
113
|
|
|
* @param string $value |
|
114
|
|
|
* @return mixed|null |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function getEntityBy(string $field, string $value) |
|
117
|
|
|
{ |
|
118
|
|
|
if (empty($value)) { |
|
119
|
|
|
return null; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$queryBuilder = $this->createQueryBuilder('main'); |
|
123
|
|
|
$queryBuilder->where( |
|
124
|
|
|
$queryBuilder->expr()->eq('main.' . $field, ':parameter') |
|
125
|
|
|
); |
|
126
|
|
|
$queryBuilder->setParameter(':parameter', $value); |
|
127
|
|
|
|
|
128
|
|
|
try { |
|
129
|
|
|
return $queryBuilder->getQuery()->getOneOrNullResult(); |
|
130
|
|
|
} catch (\Exception $exception) { |
|
131
|
|
|
return null; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $id |
|
137
|
|
|
* @return mixed|null |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getEntityByUuid(string $id) |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->getEntityBy('uuid', $id); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected function getSingleArrayResult(QueryBuilder $qb): ?array |
|
145
|
|
|
{ |
|
146
|
|
|
$response = null; |
|
|
|
|
|
|
147
|
|
|
try { |
|
148
|
|
|
$response = $qb->getQuery()->getSingleResult(Query::HYDRATE_ARRAY); |
|
149
|
|
|
} catch (NoResultException $e) { |
|
150
|
|
|
$response = null; |
|
151
|
|
|
} catch (NonUniqueResultException $e) { |
|
152
|
|
|
$response = null; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $response; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|