Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow whereNull to accept array columns argument. #2603

Merged
merged 3 commits into from Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG-2.0.md
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/database/src/Query/Builder.php
Expand Up @@ -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;
}
Expand Down
20 changes: 20 additions & 0 deletions src/database/tests/ModelRealBuilderTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions src/database/tests/QueryBuilderTest.php
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
31 changes: 31 additions & 0 deletions src/database/tests/Stubs/Model/UserRoleMorphPivot.php
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Database\Stubs\Model;

use Hyperf\DbConnection\Model\Relations\MorphPivot;

class UserRoleMorphPivot extends MorphPivot
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'user_role';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['id', 'user_id', 'role_id', 'created_at', 'updated_at'];
}