1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Oc\Entity\GeoCacheStatusEntity; |
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 CacheStatusRepository |
13
|
|
|
{ |
14
|
|
|
const TABLE = 'cache_status'; |
15
|
|
|
|
16
|
|
|
/** @var Connection */ |
17
|
|
|
private $connection; |
18
|
|
|
|
19
|
|
|
public function __construct(Connection $connection) |
20
|
|
|
{ |
21
|
|
|
$this->connection = $connection; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return GeoCacheStatusEntity[] |
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 GeoCacheStatusEntity |
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 GeoCacheStatusEntity[] |
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 GeoCacheStatusEntity |
110
|
|
|
*/ |
111
|
|
|
public function create(GeoCacheStatusEntity $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 GeoCacheStatusEntity |
131
|
|
|
*/ |
132
|
|
|
public function update(GeoCacheStatusEntity $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 GeoCacheStatusEntity |
151
|
|
|
*/ |
152
|
|
|
public function remove(GeoCacheStatusEntity $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(GeoCacheStatusEntity $entity) |
172
|
|
|
{ |
173
|
|
|
return [ |
174
|
|
|
'id' => $entity->id, |
175
|
|
|
'name' => $entity->name, |
176
|
|
|
'trans_id' => $entity->transId, |
177
|
|
|
'de' => $entity->de, |
178
|
|
|
'en' => $entity->en, |
179
|
|
|
'allow_user_view' => $entity->allowUserView, |
180
|
|
|
'allow_owner_edit_status' => $entity->allowOwnerEditStatus, |
181
|
|
|
'allow_user_log' => $entity->allowUserLog, |
182
|
|
|
]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return GeoCacheStatusEntity |
187
|
|
|
*/ |
188
|
|
|
public function getEntityFromDatabaseArray(array $data) |
189
|
|
|
{ |
190
|
|
|
$entity = new GeoCacheStatusEntity(); |
191
|
|
|
$entity->id = (int) $data['id']; |
192
|
|
|
$entity->name = (string) $data['name']; |
193
|
|
|
$entity->transId = (int) $data['trans_id']; |
194
|
|
|
$entity->de = (string) $data['de']; |
195
|
|
|
$entity->en = (string) $data['en']; |
196
|
|
|
$entity->allowUserView = (int) $data['allow_user_view']; |
197
|
|
|
$entity->allowOwnerEditStatus = (int) $data['allow_owner_edit_status']; |
198
|
|
|
$entity->allowUserLog = (int) $data['allow_user_log']; |
199
|
|
|
|
200
|
|
|
return $entity; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
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.