|
35 | 35 | use ElementaryFramework\Annotations\Annotations;
|
36 | 36 | use ElementaryFramework\LightQL\LightQL;
|
37 | 37 | use ElementaryFramework\LightQL\Persistence\PersistenceUnit;
|
| 38 | +use ElementaryFramework\LightQL\Exceptions\EntityException; |
38 | 39 |
|
39 | 40 | /**
|
40 | 41 | * Entity Manager
|
@@ -150,12 +151,21 @@ public function persist(Entity &$entity)
|
150 | 151 | }
|
151 | 152 | }
|
152 | 153 |
|
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(); |
156 | 167 |
|
157 |
| - if ($autoIncrementProperty !== null) { |
158 |
| - $entity->$autoIncrementProperty = $this->_lightql->lastInsertID(); |
| 168 | + throw new EntityException($e->getMessage()); |
159 | 169 | }
|
160 | 170 | }
|
161 | 171 |
|
@@ -184,10 +194,19 @@ public function merge(Entity &$entity)
|
184 | 194 | }
|
185 | 195 | }
|
186 | 196 |
|
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 | + } |
191 | 210 | }
|
192 | 211 |
|
193 | 212 | /**
|
@@ -217,19 +236,31 @@ public function delete(Entity &$entity)
|
217 | 236 | }
|
218 | 237 | }
|
219 | 238 |
|
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(); |
224 | 245 |
|
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 | + } |
228 | 250 | }
|
| 251 | + |
| 252 | + $this->_lightql->commit(); |
| 253 | + } catch (\Exception $e) { |
| 254 | + $this->_lightql->rollback(); |
| 255 | + |
| 256 | + throw new EntityException($e->getMessage()); |
229 | 257 | }
|
230 | 258 | }
|
231 | 259 |
|
232 | 260 | /**
|
| 261 | + * Gets the LightQL instance associated |
| 262 | + * to this entity manager. |
| 263 | + * |
233 | 264 | * @return LightQL
|
234 | 265 | */
|
235 | 266 | public function getLightQL(): LightQL
|
|
0 commit comments