Skip to content

Commit

Permalink
Added Hyperf\Database\Model\Relations\Relation::getMorphAlias(). (#…
Browse files Browse the repository at this point in the history
…7025)



Co-authored-by: 李铭昕 <[email protected]>
  • Loading branch information
People-Sea and limingxinleo authored Sep 26, 2024
1 parent fe8d71d commit 197ca2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/Model/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,14 @@ public function getRelationCountHash(bool $incrementJoinCount = true)
return 'hyperf_reserved_' . ($incrementJoinCount ? static::$selfJoinCount++ : static::$selfJoinCount);
}

/**
* Get the alias associated with a custom polymorphic class.
*/
public static function getMorphAlias(string $className): string
{
return array_search($className, static::$morphMap, strict: true) ?: $className;
}

/**
* Get all of the primary keys for an array of models.
*
Expand Down
16 changes: 12 additions & 4 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,11 @@ class ModelTest extends TestCase

protected function setUp(): void
{
parent::setUp();

Carbon::setTestNow(Carbon::now());
}

protected function tearDown(): void
{
parent::tearDown();

Mockery::close();
Carbon::setTestNow(null);

Expand Down Expand Up @@ -2075,6 +2071,18 @@ public function testUuid()
$this->assertTrue(Str::isUuid($model->newUniqueId()));
}

public function testGetMorphAlias()
{
Relation::morphMap(['user' => ModelStub::class]);

try {
$this->assertSame('user', Relation::getMorphAlias(ModelStub::class));
$this->assertSame('Does\Not\Exist', Relation::getMorphAlias('Does\Not\Exist'));
} finally {
Relation::morphMap([], false);
}
}

protected function getContainer()
{
$container = Mockery::mock(ContainerInterface::class);
Expand Down

0 comments on commit 197ca2a

Please sign in to comment.