From b7013fb73e159ea9634210148fc49f089f088961 Mon Sep 17 00:00:00 2001 From: rlgy <1032998920@qq.com> Date: Wed, 10 Oct 2018 17:33:41 +0800 Subject: [PATCH 1/4] fix #401 db entity to array lost null value column --- src/db/src/Model.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/db/src/Model.php b/src/db/src/Model.php index 7b02d6cf6..243ef17cf 100644 --- a/src/db/src/Model.php +++ b/src/db/src/Model.php @@ -316,12 +316,7 @@ public function toArray(): array if (!\method_exists($this, $methodName)) { continue; } - - $value = $this->$methodName(); - if($value === null){ - continue; - } - $data[$propertyName] = $value; + $data[$propertyName] = $this->$methodName(); } return $data; From 6fc8a204922734524c776a559513c699b8497a0f Mon Sep 17 00:00:00 2001 From: rlgy <1032998920@qq.com> Date: Wed, 10 Oct 2018 19:35:55 +0800 Subject: [PATCH 2/4] Unit test --- src/db/test/Cases/EntityTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/db/test/Cases/EntityTest.php b/src/db/test/Cases/EntityTest.php index 2c3341e53..495728bf4 100644 --- a/src/db/test/Cases/EntityTest.php +++ b/src/db/test/Cases/EntityTest.php @@ -7,8 +7,10 @@ * @contact group@swoft.org * @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE */ + namespace SwoftTest\Db\Cases; +use Swoft\Db\Bean\Collector\EntityCollector; use SwoftTest\Db\Testing\Entity\User; /** @@ -35,6 +37,13 @@ public function testToArray() 'age' => $age, ]; $this->assertEquals($data, $array); + + # test if null value columns lost when db entity to array + $user2 = new User(); + $result = $user2->toArray(); + $columns = array_keys($result); + $data = ['id', 'name', 'age', 'sex', 'desc']; + $this->assertEquals($data, $columns); } public function testToJson() From 1912cd00e1b4499256fec6d2a6ab1fdf4a76c08a Mon Sep 17 00:00:00 2001 From: rlgy <1032998920@qq.com> Date: Wed, 10 Oct 2018 19:37:59 +0800 Subject: [PATCH 3/4] Unit test --- src/db/test/Cases/EntityTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/db/test/Cases/EntityTest.php b/src/db/test/Cases/EntityTest.php index 495728bf4..c0b06b483 100644 --- a/src/db/test/Cases/EntityTest.php +++ b/src/db/test/Cases/EntityTest.php @@ -7,10 +7,8 @@ * @contact group@swoft.org * @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE */ - namespace SwoftTest\Db\Cases; -use Swoft\Db\Bean\Collector\EntityCollector; use SwoftTest\Db\Testing\Entity\User; /** From eac3bb08860a51acc1bab14795e4d8eccd4979de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=9C=9D=E6=99=96?= Date: Wed, 10 Oct 2018 20:21:08 +0800 Subject: [PATCH 4/4] Format. --- src/db/test/Cases/EntityTest.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/db/test/Cases/EntityTest.php b/src/db/test/Cases/EntityTest.php index c0b06b483..099d6f992 100644 --- a/src/db/test/Cases/EntityTest.php +++ b/src/db/test/Cases/EntityTest.php @@ -11,8 +11,6 @@ use SwoftTest\Db\Testing\Entity\User; -/** - */ class EntityTest extends AbstractMysqlCase { public function testToArray() @@ -36,9 +34,9 @@ public function testToArray() ]; $this->assertEquals($data, $array); - # test if null value columns lost when db entity to array - $user2 = new User(); - $result = $user2->toArray(); + // test if null value columns lost when db entity to array + $user2 = new User(); + $result = $user2->toArray(); $columns = array_keys($result); $data = ['id', 'name', 'age', 'sex', 'desc']; $this->assertEquals($data, $columns);