Skip to content

Commit 6f1ebef

Browse files
committed
Use of transactions in entity manager
1 parent 0e48bd0 commit 6f1ebef

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

src/LightQL/Entities/EntityManager.php

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use ElementaryFramework\Annotations\Annotations;
3636
use ElementaryFramework\LightQL\LightQL;
3737
use ElementaryFramework\LightQL\Persistence\PersistenceUnit;
38+
use ElementaryFramework\LightQL\Exceptions\EntityException;
3839

3940
/**
4041
* Entity Manager
@@ -150,12 +151,21 @@ public function persist(Entity &$entity)
150151
}
151152
}
152153

153-
$this->_lightql
154-
->from($entityAnnotation[0]->table)
155-
->insert($fieldAndValues);
154+
$this->_lightql->beginTransaction();
155+
try {
156+
$this->_lightql
157+
->from($entityAnnotation[0]->table)
158+
->insert($fieldAndValues);
159+
160+
if ($autoIncrementProperty !== null) {
161+
$entity->$autoIncrementProperty = $this->_lightql->lastInsertID();
162+
}
163+
164+
$this->_lightql->commit();
165+
} catch (\Exception $e) {
166+
$this->_lightql->rollback();
156167

157-
if ($autoIncrementProperty !== null) {
158-
$entity->$autoIncrementProperty = $this->_lightql->lastInsertID();
168+
throw new EntityException($e->getMessage());
159169
}
160170
}
161171

@@ -184,10 +194,19 @@ public function merge(Entity &$entity)
184194
}
185195
}
186196

187-
$this->_lightql
188-
->from($entityAnnotation[0]->table)
189-
->where($where)
190-
->update($fieldAndValues);
197+
$this->_lightql->beginTransaction();
198+
try {
199+
$this->_lightql
200+
->from($entityAnnotation[0]->table)
201+
->where($where)
202+
->update($fieldAndValues);
203+
204+
$this->_lightql->commit();
205+
} catch (\Exception $e) {
206+
$this->_lightql->rollback();
207+
208+
throw new EntityException($e->getMessage());
209+
}
191210
}
192211

193212
/**
@@ -217,19 +236,31 @@ public function delete(Entity &$entity)
217236
}
218237
}
219238

220-
$this->_lightql
221-
->from($entityAnnotation[0]->table)
222-
->where($where)
223-
->delete();
239+
$this->_lightql->beginTransaction();
240+
try {
241+
$this->_lightql
242+
->from($entityAnnotation[0]->table)
243+
->where($where)
244+
->delete();
224245

225-
if (count($pk) > 0) {
226-
foreach ($pk as $item) {
227-
$entity->$item = null;
246+
if (count($pk) > 0) {
247+
foreach ($pk as $item) {
248+
$entity->$item = null;
249+
}
228250
}
251+
252+
$this->_lightql->commit();
253+
} catch (\Exception $e) {
254+
$this->_lightql->rollback();
255+
256+
throw new EntityException($e->getMessage());
229257
}
230258
}
231259

232260
/**
261+
* Gets the LightQL instance associated
262+
* to this entity manager.
263+
*
233264
* @return LightQL
234265
*/
235266
public function getLightQL(): LightQL

src/LightQL/LightQL.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,21 @@ public function quote($value): string
986986
return $this->_pdo->quote($value);
987987
}
988988

989+
public function beginTransaction(): bool
990+
{
991+
return $this->_pdo->beginTransaction();
992+
}
993+
994+
public function commit(): bool
995+
{
996+
return $this->_pdo->commit();
997+
}
998+
999+
public function rollback(): bool
1000+
{
1001+
return $this->_pdo->rollBack();
1002+
}
1003+
9891004
/**
9901005
* Converts a value to a string.
9911006
*

0 commit comments

Comments
 (0)