From 085e880631612727a1a223f4b0c2e12a1a2c1c8e Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sat, 7 Aug 2021 18:19:01 +0000 Subject: [PATCH] [CORE][Entity] Fix implementation of createOrUpdate so it doesn't throw NotFoundException if trying to create an object This was previously done because we wanted to notify the callee that and entity existed but not with the provided contents. With the change of return value, with a bool $is_update, this is no longer a problem. --- src/Core/Entity.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Core/Entity.php b/src/Core/Entity.php index b5303dbebf..1a5d1266a5 100644 --- a/src/Core/Entity.php +++ b/src/Core/Entity.php @@ -63,7 +63,7 @@ abstract class Entity foreach ($args as $prop => $val) { if (property_exists($class, $prop)) { - $set = 'set' . Formatting::snakeCaseToCamelCase($prop); + $set = 'set' . ucfirst(Formatting::snakeCaseToCamelCase($prop)); $obj->{$set}($val); } else { Log::error($m = "Property {$class}::{$prop} doesn't exist"); @@ -75,12 +75,22 @@ abstract class Entity /** * Create a new instance, but check for duplicates + * + * @return [$obj, $is_update] */ public static function createOrUpdate(array $args, array $find_by_keys = []) { $table = DB::getTableForClass(get_called_class()); $find_by = $find_by_keys == [] ? $args : array_intersect_key($args, array_flip($find_by_keys)); - return self::create($args, DB::findOneBy($table, $find_by)); + try { + $obj = DB::findOneBy($table, $find_by); + } catch (NotFoundException) { + $obj = null; + } catch (\Exception $e) { + Log::unexpected_exception($e); + } + $is_update = $obj !== null; + return [self::create($args, $obj), $is_update]; } /**