Skip to content

Commit ab3c9a7

Browse files
Support using model name like App\\Model\\User for validatoin rule exists and unique. (#7459)
Co-authored-by: 李铭昕 <715557344@qq.com>
1 parent dfe1260 commit ab3c9a7

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/Rules/DatabaseRule.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
namespace Hyperf\Validation\Rules;
1414

1515
use Closure;
16+
use Hyperf\Database\Model\Model;
1617

1718
use function Hyperf\Collection\collect;
19+
use function Hyperf\Support\make;
1820

1921
trait DatabaseRule
2022
{
@@ -36,6 +38,29 @@ trait DatabaseRule
3638
*/
3739
public function __construct(protected string $table, protected string $column = 'NULL')
3840
{
41+
$this->table = $this->resolveTableName($table);
42+
}
43+
44+
/**
45+
* Resolves the name of the table from the given string.
46+
*/
47+
public function resolveTableName(string $table): string
48+
{
49+
if (! str_contains($table, '\\') || ! class_exists($table)) {
50+
return $table;
51+
}
52+
53+
if (is_subclass_of($table, Model::class)) {
54+
/** @var Model $model */
55+
$model = make($table);
56+
if ($connection = $model->getConnectionName()) {
57+
return $connection . '.' . $model->getTable();
58+
}
59+
60+
return $model->getTable();
61+
}
62+
63+
return $table;
3964
}
4065

4166
/**

tests/Cases/ValidationExistsRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ protected function setUp(): void
6565
ApplicationContext::setContainer($container);
6666
Register::setConnectionResolver($resolver);
6767
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn(new EventDispatcher());
68+
$container->shouldReceive('make')->with(UserWithConnection::class)->andReturn(new UserWithConnection());
6869

6970
$this->createSchema();
7071
}
@@ -88,6 +89,10 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
8889
$rule = new Exists('table', 'column');
8990
$rule->where('foo', 'bar');
9091
$this->assertEquals('exists:table,column,foo,"bar"', (string) $rule);
92+
93+
$rule = new Exists(UserWithConnection::class, 'column');
94+
$rule->where('foo', 'bar');
95+
$this->assertSame('exists:mysql.users,column,foo,"bar"', (string) $rule);
9196
}
9297

9398
public function testItChoosesValidRecordsUsingWhereInRule()
@@ -222,3 +227,8 @@ class DatabaseTestUser extends Model
222227

223228
protected array $guarded = [];
224229
}
230+
231+
class UserWithConnection extends DatabaseTestUser
232+
{
233+
protected ?string $connection = 'mysql';
234+
}

tests/Cases/ValidationUniqueRuleTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212

1313
namespace HyperfTest\Validation\Cases;
1414

15+
use Hyperf\Context\ApplicationContext;
1516
use Hyperf\Database\Model\Model;
1617
use Hyperf\Validation\Rules\Unique;
18+
use Mockery as m;
1719
use PHPUnit\Framework\Attributes\CoversNothing;
1820
use PHPUnit\Framework\TestCase;
21+
use Psr\Container\ContainerInterface;
1922

2023
/**
2124
* @internal
@@ -24,6 +27,21 @@
2427
#[CoversNothing]
2528
class ValidationUniqueRuleTest extends TestCase
2629
{
30+
protected function setUp(): void
31+
{
32+
$container = m::mock(ContainerInterface::class);
33+
$container->shouldReceive('make')
34+
->with(DatabaseModelWithConnection::class)
35+
->andReturn(new DatabaseModelWithConnection());
36+
37+
ApplicationContext::setContainer($container);
38+
}
39+
40+
protected function tearDown(): void
41+
{
42+
m::close();
43+
}
44+
2745
public function testItCorrectlyFormatsAStringVersionOfTheRule()
2846
{
2947
$rule = new Unique('table');
@@ -66,6 +84,10 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
6684
$rule = new Unique('table');
6785
$rule->where('foo', 1);
6886
$this->assertEquals('unique:table,NULL,NULL,id,foo,"1"', (string) $rule);
87+
88+
$rule = new Unique(DatabaseModelWithConnection::class, 'column');
89+
$rule->where('foo', 'bar');
90+
$this->assertSame('unique:mysql.table,column,NULL,id,foo,"bar"', (string) $rule);
6991
}
7092
}
7193

@@ -75,3 +97,10 @@ class DatabaseModelStub extends Model
7597

7698
protected array $guarded = [];
7799
}
100+
101+
class DatabaseModelWithConnection extends DatabaseModelStub
102+
{
103+
protected ?string $table = 'table';
104+
105+
protected ?string $connection = 'mysql';
106+
}

0 commit comments

Comments
 (0)