From 5f433f975cdb26da86ee774153bf223d764fcfc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Wed, 30 Sep 2020 17:29:51 +0800 Subject: [PATCH 1/3] Fixed the error sql when the column is null. --- src/database/src/Query/Builder.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/database/src/Query/Builder.php b/src/database/src/Query/Builder.php index 5ac5438b5f..7c3d69d1b3 100755 --- a/src/database/src/Query/Builder.php +++ b/src/database/src/Query/Builder.php @@ -884,16 +884,18 @@ public function whereIntegerNotInRaw($column, $values, $boolean = 'and') /** * Add a "where null" clause to the query. * - * @param string $column + * @param array|string $columns * @param string $boolean * @param bool $not * @return $this */ - public function whereNull($column, $boolean = 'and', $not = false) + public function whereNull($columns, $boolean = 'and', $not = false) { $type = $not ? 'NotNull' : 'Null'; - $this->wheres[] = compact('type', 'column', 'boolean'); + foreach (Arr::wrap($columns) as $column) { + $this->wheres[] = compact('type', 'column', 'boolean'); + } return $this; } From 4bdf2a131ecfc19660c258d44d32cee7d0a2a065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Wed, 30 Sep 2020 17:37:51 +0800 Subject: [PATCH 2/3] Added test cases. --- CHANGELOG-2.0.md | 4 ++++ src/database/tests/QueryBuilderTest.php | 26 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index c439d5327e..333c977e88 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -5,6 +5,10 @@ - [#2594](https://github.com/hyperf/hyperf/pull/2594) Fixed crontab does not stops when using signal. - [#2601](https://github.com/hyperf/hyperf/pull/2601) Fixed `@property` will be replaced by `@property-read` when the property has `getter` and `setter` at the same time. +## Optimized + +- [#2603](https://github.com/hyperf/hyperf/pull/2603) Allow `whereNull` to accept array columns argument. + # v2.0.13 - 2020-09-28 ## Added diff --git a/src/database/tests/QueryBuilderTest.php b/src/database/tests/QueryBuilderTest.php index 5303b08471..a32ae645c2 100644 --- a/src/database/tests/QueryBuilderTest.php +++ b/src/database/tests/QueryBuilderTest.php @@ -768,6 +768,19 @@ public function testBasicWhereNulls() $this->assertEquals([0 => 1], $builder->getBindings()); } + public function testArrayWhereNulls() + { + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->whereNull(['id', 'expires_at']); + $this->assertSame('select * from "users" where "id" is null and "expires_at" is null', $builder->toSql()); + $this->assertEquals([], $builder->getBindings()); + + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->where('id', '=', 1)->orWhereNull(['id', 'expires_at']); + $this->assertSame('select * from "users" where "id" = ? or "id" is null or "expires_at" is null', $builder->toSql()); + $this->assertEquals([0 => 1], $builder->getBindings()); + } + public function testBasicWhereNotNulls() { $builder = $this->getBuilder(); @@ -781,6 +794,19 @@ public function testBasicWhereNotNulls() $this->assertEquals([0 => 1], $builder->getBindings()); } + public function testArrayWhereNotNulls() + { + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->whereNotNull(['id', 'expires_at']); + $this->assertSame('select * from "users" where "id" is not null and "expires_at" is not null', $builder->toSql()); + $this->assertEquals([], $builder->getBindings()); + + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->where('id', '>', 1)->orWhereNotNull(['id', 'expires_at']); + $this->assertSame('select * from "users" where "id" > ? or "id" is not null or "expires_at" is not null', $builder->toSql()); + $this->assertEquals([0 => 1], $builder->getBindings()); + } + public function testGroupBys() { $builder = $this->getBuilder(); From b2cc1b5f8defa30a31a185e5d89c65a4d52a5f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Wed, 30 Sep 2020 21:03:11 +0800 Subject: [PATCH 3/3] Update --- src/database/tests/ModelRealBuilderTest.php | 20 ++++++++++++ .../tests/Stubs/Model/UserRoleMorphPivot.php | 31 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/database/tests/Stubs/Model/UserRoleMorphPivot.php diff --git a/src/database/tests/ModelRealBuilderTest.php b/src/database/tests/ModelRealBuilderTest.php index a20bdb90c3..d6ce414014 100644 --- a/src/database/tests/ModelRealBuilderTest.php +++ b/src/database/tests/ModelRealBuilderTest.php @@ -18,6 +18,7 @@ use HyperfTest\Database\Stubs\Model\User; use HyperfTest\Database\Stubs\Model\UserExtCamel; use HyperfTest\Database\Stubs\Model\UserRole; +use HyperfTest\Database\Stubs\Model\UserRoleMorphPivot; use HyperfTest\Database\Stubs\Model\UserRolePivot; use Mockery; use PHPUnit\Framework\TestCase; @@ -150,6 +151,25 @@ public function testCamelCaseGetModel() } } + public function testSaveMorphPivot() + { + $this->getContainer(); + $pivot = UserRoleMorphPivot::query()->find(1); + $pivot->created_at = $now = Carbon::now(); + $pivot->save(); + + $sqls = [ + ['select * from `user_role` where `user_role`.`id` = ? limit 1', [1]], + ['update `user_role` set `created_at` = ?, `user_role`.`updated_at` = ? where `id` = ?', [$now->toDateTimeString(), $now->toDateTimeString(), 1]], + ]; + + while ($event = $this->channel->pop(0.001)) { + if ($event instanceof QueryExecuted) { + $this->assertSame([$event->sql, $event->bindings], array_shift($sqls)); + } + } + } + protected function getContainer() { $dispatcher = Mockery::mock(EventDispatcherInterface::class); diff --git a/src/database/tests/Stubs/Model/UserRoleMorphPivot.php b/src/database/tests/Stubs/Model/UserRoleMorphPivot.php new file mode 100644 index 0000000000..bd1e755c07 --- /dev/null +++ b/src/database/tests/Stubs/Model/UserRoleMorphPivot.php @@ -0,0 +1,31 @@ +