1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Oc\Entity\GeoCacheSizeEntity; |
7
|
|
|
use Oc\Repository\Exception\RecordAlreadyExistsException; |
8
|
|
|
use Oc\Repository\Exception\RecordNotFoundException; |
9
|
|
|
use Oc\Repository\Exception\RecordNotPersistedException; |
10
|
|
|
use Oc\Repository\Exception\RecordsNotFoundException; |
11
|
|
|
|
12
|
|
|
class CacheSizeRepository |
13
|
|
|
{ |
14
|
|
|
const TABLE = 'cache_size'; |
15
|
|
|
|
16
|
|
|
/** @var Connection */ |
17
|
|
|
private $connection; |
18
|
|
|
|
19
|
|
|
public function __construct(Connection $connection) |
20
|
|
|
{ |
21
|
|
|
$this->connection = $connection; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return GeoCacheSizeEntity[] |
26
|
|
|
*/ |
27
|
|
|
public function fetchAll() |
28
|
|
|
{ |
29
|
|
|
$statement = $this->connection->createQueryBuilder() |
30
|
|
|
->select('*') |
31
|
|
|
->from(self::TABLE) |
32
|
|
|
->execute(); |
33
|
|
|
|
34
|
|
|
$result = $statement->fetchAll(); |
35
|
|
|
|
36
|
|
|
if ($statement->rowCount() === 0) { |
37
|
|
|
throw new RecordsNotFoundException('No records found'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$records = []; |
41
|
|
|
|
42
|
|
|
foreach ($result as $item) { |
43
|
|
|
$records[] = $this->getEntityFromDatabaseArray($item); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $records; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return GeoCacheSizeEntity |
51
|
|
|
*/ |
52
|
|
|
public function fetchOneBy(array $where = []) |
53
|
|
|
{ |
54
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
55
|
|
|
->select('*') |
56
|
|
|
->from(self::TABLE) |
57
|
|
|
->setMaxResults(1); |
58
|
|
|
|
59
|
|
|
if (count($where) > 0) { |
60
|
|
|
foreach ($where as $column => $value) { |
61
|
|
|
$queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value)); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$statement = $queryBuilder->execute(); |
66
|
|
|
|
67
|
|
|
$result = $statement->fetch(); |
68
|
|
|
|
69
|
|
|
if ($statement->rowCount() === 0) { |
70
|
|
|
throw new RecordNotFoundException('Record with given where clause not found'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->getEntityFromDatabaseArray($result); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return GeoCacheSizeEntity[] |
78
|
|
|
*/ |
79
|
|
|
public function fetchBy(array $where = []) |
80
|
|
|
{ |
81
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
82
|
|
|
->select('*') |
83
|
|
|
->from(self::TABLE); |
84
|
|
|
|
85
|
|
|
if (count($where) > 0) { |
86
|
|
|
foreach ($where as $column => $value) { |
87
|
|
|
$queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value)); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$statement = $queryBuilder->execute(); |
92
|
|
|
|
93
|
|
|
$result = $statement->fetchAll(); |
94
|
|
|
|
95
|
|
|
if ($statement->rowCount() === 0) { |
96
|
|
|
throw new RecordsNotFoundException('No records with given where clause found'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$entities = []; |
100
|
|
|
|
101
|
|
|
foreach ($result as $item) { |
102
|
|
|
$entities[] = $this->getEntityFromDatabaseArray($item); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $entities; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return GeoCacheSizeEntity |
110
|
|
|
*/ |
111
|
|
|
public function create(GeoCacheSizeEntity $entity) |
112
|
|
|
{ |
113
|
|
|
if (!$entity->isNew()) { |
114
|
|
|
throw new RecordAlreadyExistsException('The entity does already exist.'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$databaseArray = $this->getDatabaseArrayFromEntity($entity); |
118
|
|
|
|
119
|
|
|
$this->connection->insert( |
120
|
|
|
self::TABLE, |
121
|
|
|
$databaseArray |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$entity->id = (int) $this->connection->lastInsertId(); |
125
|
|
|
|
126
|
|
|
return $entity; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return GeoCacheSizeEntity |
131
|
|
|
*/ |
132
|
|
|
public function update(GeoCacheSizeEntity $entity) |
133
|
|
|
{ |
134
|
|
|
if ($entity->isNew()) { |
135
|
|
|
throw new RecordNotPersistedException('The entity does not exist.'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$databaseArray = $this->getDatabaseArrayFromEntity($entity); |
139
|
|
|
|
140
|
|
|
$this->connection->update( |
141
|
|
|
self::TABLE, |
142
|
|
|
$databaseArray, |
143
|
|
|
['id' => $entity->id] |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
return $entity; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return GeoCacheSizeEntity |
151
|
|
|
*/ |
152
|
|
|
public function remove(GeoCacheSizeEntity $entity) |
153
|
|
|
{ |
154
|
|
|
if ($entity->isNew()) { |
155
|
|
|
throw new RecordNotPersistedException('The entity does not exist.'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$this->connection->delete( |
159
|
|
|
self::TABLE, |
160
|
|
|
['id' => $entity->id] |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
$entity->cacheId = null; |
|
|
|
|
164
|
|
|
|
165
|
|
|
return $entity; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return [] |
|
|
|
|
170
|
|
|
*/ |
171
|
|
|
public function getDatabaseArrayFromEntity(GeoCacheSizeEntity $entity) |
172
|
|
|
{ |
173
|
|
|
return [ |
174
|
|
|
'id' => $entity->id, |
175
|
|
|
'name' => $entity->name, |
176
|
|
|
'trans_id' => $entity->transId, |
177
|
|
|
'ordinal' => $entity->ordinal, |
178
|
|
|
'de' => $entity->de, |
179
|
|
|
'en' => $entity->en, |
180
|
|
|
]; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return GeoCacheSizeEntity |
185
|
|
|
*/ |
186
|
|
|
public function getEntityFromDatabaseArray(array $data) |
187
|
|
|
{ |
188
|
|
|
$entity = new GeoCacheSizeEntity(); |
189
|
|
|
$entity->id = (int) $data['id']; |
190
|
|
|
$entity->name = (string) $data['name']; |
191
|
|
|
$entity->transId = (int) $data['trans_id']; |
192
|
|
|
$entity->ordinal = (int) $data['ordinal']; |
193
|
|
|
$entity->de = (string) $data['de']; |
194
|
|
|
$entity->en = (string) $data['en']; |
195
|
|
|
|
196
|
|
|
return $entity; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.