@@ -92,7 +92,7 @@ public function count(string $column = '*')
9292 if ($ this ->definition ->getDeletionTimestamp ()) {
9393 $ this ->isNull ($ this ->prefixTableNameTo ($ this ->definition ->getDeletionTimestamp ()));
9494 }
95-
95+
9696 return parent ::count ($ column );
9797 }
9898
@@ -124,10 +124,7 @@ public function insert(array $data)
124124 unset($ base [$ this ->definition ->getPrimaryKey ()[0 ]]);
125125 }
126126
127- if (!parent ::insert ($ base )) {
128- // Transaction already cancelled by the statement handler
129- return false ;
130- }
127+ parent ::insert ($ base );
131128
132129 if ($ this ->definition ->isAutoIncrement ()) {
133130 $ this ->lastId = $ this ->db ->getLastId ();
@@ -154,10 +151,7 @@ public function insert(array $data)
154151 foreach ($ items as $ item ) {
155152 $ item [$ property ->getForeignColumn ()] = $ data [$ property ->getLocalColumn ()];
156153
157- if (!$ mapping ->insert ($ item )) {
158- // Transaction already cancelled by the statement handler
159- return false ;
160- }
154+ $ mapping ->insert ($ item );
161155 }
162156 }
163157
@@ -177,6 +171,7 @@ public function insert(array $data)
177171 *
178172 * @param array $data
179173 * @return boolean
174+ * @throws MappingException
180175 */
181176 public function update (array $ data = array ())
182177 {
@@ -188,7 +183,7 @@ public function update(array $data = array())
188183
189184 foreach ($ primaryKey as $ column ) {
190185 if (!array_key_exists ($ column , $ data )) {
191- return false ;
186+ throw new MappingException ( ' Failed to update record. Missing primary key column: ' . $ column ) ;
192187 }
193188
194189 if (is_null ($ data [$ column ])) {
@@ -199,7 +194,7 @@ public function update(array $data = array())
199194 }
200195
201196 if (!$ original = $ this ->findOne ()) {
202- return false ;
197+ throw new MappingException ( ' Failed to update record. Original not found. ' ) ;
203198 }
204199
205200 $ useTransaction = !$ this ->db ->getConnection ()->inTransaction ();
@@ -222,12 +217,12 @@ public function update(array $data = array())
222217 ]);
223218
224219 return true ;
225- } catch (\Exception $ e ) {
220+ } catch (\Exception $ exception ) {
226221 if ($ useTransaction ) {
227222 $ this ->db ->cancelTransaction ();
228223 }
229224
230- return false ;
225+ throw $ exception ;
231226 }
232227 }
233228
@@ -236,14 +231,15 @@ public function update(array $data = array())
236231 *
237232 * @param array $data
238233 * @return bool
234+ * @throws MappingException
239235 */
240236 public function save (array $ data )
241237 {
242238 $ primaryKey = $ this ->definition ->getPrimaryKey ();
243239
244240 foreach ($ primaryKey as $ column ) {
245241 if (!array_key_exists ($ column , $ data )) {
246- return false ;
242+ throw new MappingException ( ' Failed to save record. Missing primary key column: ' . $ column ) ;
247243 }
248244
249245 if (is_null ($ data [$ column ])) {
@@ -261,6 +257,7 @@ public function save(array $data)
261257 * Removes data matching the condition.
262258 *
263259 * @return bool
260+ * @throws MappingException
264261 */
265262 public function remove ()
266263 {
@@ -271,20 +268,30 @@ public function remove()
271268 $ ids = $ this ->collectPrimary ($ original , $ ids );
272269 }
273270
274- try {
271+ $ useTransaction = !$ this ->db ->getConnection ()->inTransaction ();
272+
273+ if ($ useTransaction ) {
275274 $ this ->db ->startTransaction ();
275+ }
276+
277+ try {
276278 $ this ->delete ($ ids );
277279
278- $ this ->db ->closeTransaction ();
280+ if ($ useTransaction ) {
281+ $ this ->db ->closeTransaction ();
282+ }
279283
280284 foreach ($ data as $ item ) {
281285 $ this ->dispatch ('removed ' , $ item );
282286 }
283287
284288 return true ;
285289 } catch (\Exception $ exception ) {
286- $ this ->db ->cancelTransaction ();
287- return false ;
290+ if ($ useTransaction ) {
291+ $ this ->db ->cancelTransaction ();
292+ }
293+
294+ throw $ exception ;
288295 }
289296 }
290297
@@ -296,7 +303,7 @@ public function remove()
296303 * @param array $original
297304 * @param array $deleteIds
298305 * @return array
299- * @throws \Exception
306+ * @throws MappingException
300307 */
301308 private function replace (array $ data , array $ original , array $ deleteIds = [])
302309 {
@@ -331,9 +338,7 @@ private function replace(array $data, array $original, array $deleteIds = [])
331338 $ this ->definition ->getModificationData ()
332339 );
333340
334- if (!$ query ->update ($ base )) {
335- throw new \Exception ('Failed to update record. ' );
336- }
341+ $ query ->update ($ base );
337342 }
338343
339344 foreach ($ this ->definition ->getProperties () as $ property ) {
@@ -361,9 +366,7 @@ private function replace(array $data, array $original, array $deleteIds = [])
361366 $ item [$ property ->getForeignColumn ()] = $ data [$ property ->getLocalColumn ()];
362367 }
363368
364- if (!$ mapping ->insert ($ item )) {
365- throw new \Exception ('Failed to insert record. ' );
366- }
369+ $ mapping ->insert ($ item );
367370 }
368371
369372 foreach ($ delete as $ item ) {
@@ -380,7 +383,7 @@ private function replace(array $data, array $original, array $deleteIds = [])
380383 continue ;
381384 }
382385
383- $ originalItem = Collection::first ($ propertyOriginal , function ($ original ) use ($ item , $ propertyPrimary ) {
386+ $ originalItem = Collection::first ($ propertyOriginal , function ($ original ) use ($ item , $ propertyPrimary ) {
384387 foreach ($ propertyPrimary as $ column ) {
385388 if ($ original [$ column ] != $ item [$ column ]) {
386389 return false ;
@@ -402,14 +405,14 @@ private function replace(array $data, array $original, array $deleteIds = [])
402405 * mapping tables to primary key values.
403406 *
404407 * @param array $ids
405- * @throws \Exception
408+ * @throws MappingException
406409 */
407410 private function delete ($ ids = [])
408411 {
409412 foreach ($ ids as $ table => $ deleteColumns ) {
410413 foreach ($ deleteColumns as $ deletion => $ deletionContext ) {
411414 // Arrange values into groups based on all but the last key
412- $ grouped = Collection::group ($ deletionContext ['keys ' ], function (array $ keys ) {
415+ $ grouped = Collection::group ($ deletionContext ['keys ' ], function (array $ keys ) {
413416 array_pop ($ keys );
414417 return implode (': ' , $ keys );
415418 });
@@ -465,7 +468,7 @@ private function delete($ids = [])
465468 }
466469
467470 if (!$ result ) {
468- throw new \ Exception ('Failed to delete records. ' );
471+ throw new MappingException ('Failed to delete records. ' );
469472 }
470473 }
471474 }
@@ -549,7 +552,7 @@ private function map(array $data)
549552 $ results = $ mapping
550553 ->findAll ();
551554
552- $ properties [$ property ->getName ()] = Collection::group ($ results , function ($ result ) use ($ groupColumn ) {
555+ $ properties [$ property ->getName ()] = Collection::group ($ results , function ($ result ) use ($ groupColumn ) {
553556 return $ result [$ groupColumn ];
554557 });
555558 }
@@ -657,7 +660,7 @@ private function dispatch(string $event, array $data, $args = [])
657660 private function requiresPrefix (string $ column ): bool
658661 {
659662 $ pattern = '/^[a-zA-Z_][a-zA-Z0-9_]*$/ ' ;
660- return (bool ) preg_match ($ pattern , $ column );
663+ return (bool )preg_match ($ pattern , $ column );
661664 }
662665
663666 /**
@@ -674,7 +677,8 @@ private function requiresPrefix(string $column): bool
674677 *
675678 * @return string | array
676679 */
677- private function prefixTableNameTo ($ input ) {
680+ private function prefixTableNameTo ($ input )
681+ {
678682 $ table = $ this ->definition ->getTable ();
679683
680684 if (is_string ($ input )) {
0 commit comments