From 1f89ad9d2a887c203718e780a690cccce0f4383c Mon Sep 17 00:00:00 2001 From: wfuren <593374099@qq.com> Date: Fri, 20 Apr 2018 09:30:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=9E=E4=BD=93=E7=B1=BB=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=9A=84=E6=97=B6=E5=80=99=E4=B8=8D=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E4=B8=BAstring=E7=84=B6=E5=90=8ENULL?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=EF=BC=8C=E6=9F=A5=E8=AF=A2=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E5=80=99=E5=87=BA=E7=8E=B0=E7=BC=BA=E5=A4=B1=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/src/Helper/EntityHelper.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/db/src/Helper/EntityHelper.php b/src/db/src/Helper/EntityHelper.php index cded85d..24fdab5 100644 --- a/src/db/src/Helper/EntityHelper.php +++ b/src/db/src/Helper/EntityHelper.php @@ -55,8 +55,12 @@ public static function arrayToEntity(array $data, string $className) } $field = $entities[$className]['column'][$col]; - $setterMethod = 'set' . ucfirst($field); - + $function = explode('_', $field); + $function = array_map(function ($word) { + return ucfirst($word); + }, $function); + $setterMethod = 'set' . implode('', $function); + $type = $entities[$className]['field'][$field]['type']; $value = self::trasferTypes($type, $value); @@ -83,7 +87,7 @@ public static function trasferTypes($type, $value) if ($type === Types::INT || $type === Types::NUMBER) { $value = (int)$value; } elseif ($type === Types::STRING) { - $value = (string)$value; + $value = is_null($value) ? (unset)$value : (string)$value; } elseif ($type === Types::BOOLEAN) { $value = (bool)$value; } elseif ($type === Types::FLOAT) { From 2491f9bbf74d822a3a1440b633296f1bb2fa5236 Mon Sep 17 00:00:00 2001 From: Inhere Date: Sat, 21 Apr 2018 10:26:49 +0800 Subject: [PATCH 2/2] Update EntityHelper.php --- src/db/src/Helper/EntityHelper.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/db/src/Helper/EntityHelper.php b/src/db/src/Helper/EntityHelper.php index 24fdab5..f936c64 100644 --- a/src/db/src/Helper/EntityHelper.php +++ b/src/db/src/Helper/EntityHelper.php @@ -12,6 +12,7 @@ use Swoft\Db\Bean\Collector\EntityCollector; use Swoft\Db\Exception\DbException; use Swoft\Db\Types; +use Swoft\Helper\StringHelper; /** * EntityHelper @@ -55,21 +56,17 @@ public static function arrayToEntity(array $data, string $className) } $field = $entities[$className]['column'][$col]; - $function = explode('_', $field); - $function = array_map(function ($word) { - return ucfirst($word); - }, $function); - $setterMethod = 'set' . implode('', $function); + $setterMethod = StringHelper::camel('set_' . $field); $type = $entities[$className]['field'][$field]['type']; $value = self::trasferTypes($type, $value); - if (method_exists($object, $setterMethod)) { + if (\method_exists($object, $setterMethod)) { $attrs[$field] = $value; $object->$setterMethod($value); } } - if (method_exists($object, 'setAttrs')) { + if (\method_exists($object, 'setAttrs')) { $object->setAttrs($attrs); } @@ -87,7 +84,7 @@ public static function trasferTypes($type, $value) if ($type === Types::INT || $type === Types::NUMBER) { $value = (int)$value; } elseif ($type === Types::STRING) { - $value = is_null($value) ? (unset)$value : (string)$value; + $value = null === $value ? null : (string)$value; } elseif ($type === Types::BOOLEAN) { $value = (bool)$value; } elseif ($type === Types::FLOAT) { @@ -128,7 +125,7 @@ public static function transferParameter($key, $value, $type): array */ private static function formatParamsKey($key): string { - if (\is_string($key) && strpos($key, ':') === false) { + if (\is_string($key) && \strpos($key, ':') === false) { return ':' . $key; }