-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(database): add explicit relation attributes (#874)
- Loading branch information
1 parent
3279ac3
commit 5e4df24
Showing
9 changed files
with
309 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Database; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
final readonly class BelongsTo | ||
{ | ||
public function __construct(public string $localPropertyName, public string $inversePropertyName = 'id') | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Database; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
final readonly class HasMany | ||
{ | ||
/** @param null|class-string $inverseClassName */ | ||
public function __construct( | ||
public string $inversePropertyName, | ||
public ?string $inverseClassName = null, | ||
public string $localPropertyName = 'id', | ||
) { | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Tempest/Database/tests/Relations/BelongsToRelationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Database\Tests\Relations; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Tempest\Database\Builder\ModelDefinition; | ||
use Tempest\Database\Tests\Relations\Fixtures\BelongsToParentModel; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class BelongsToRelationTest extends TestCase | ||
{ | ||
public function test_inferred_belongs_to_relation(): void | ||
{ | ||
$definition = new ModelDefinition(BelongsToParentModel::class); | ||
$inferredRelation = $definition->getRelations('relatedModel'); | ||
|
||
$this->assertCount(1, $inferredRelation); | ||
$this->assertSame('belongs_to_parent_model.relatedModel', $inferredRelation[0]->getRelationName()); | ||
$this->assertEquals( | ||
'LEFT JOIN `belongs_to_related` AS `belongs_to_parent_model.relatedModel`' . | ||
' ON `belongs_to_parent_model`.`relatedModel_id` = `belongs_to_parent_model.relatedModel`.`id`', | ||
$inferredRelation[0]->getStatement(), | ||
); | ||
} | ||
|
||
public function test_attribute_with_default_belongs_to_relation(): void | ||
{ | ||
$definition = new ModelDefinition(BelongsToParentModel::class); | ||
$namedRelation = $definition->getRelations('otherRelatedModel'); | ||
|
||
$this->assertCount(1, $namedRelation); | ||
|
||
$this->assertSame('belongs_to_parent_model.otherRelatedModel', $namedRelation[0]->getRelationName()); | ||
$this->assertEquals( | ||
'LEFT JOIN `belongs_to_related` AS `belongs_to_parent_model.otherRelatedModel`' . | ||
' ON `belongs_to_parent_model`.`other_id` = `belongs_to_parent_model.otherRelatedModel`.`id`', | ||
$namedRelation[0]->getStatement(), | ||
); | ||
} | ||
|
||
public function test_attribute_belongs_to_relation(): void | ||
{ | ||
$definition = new ModelDefinition(BelongsToParentModel::class); | ||
$doublyNamedRelation = $definition->getRelations('stillOtherRelatedModel'); | ||
|
||
$this->assertCount(1, $doublyNamedRelation); | ||
|
||
$this->assertSame('belongs_to_parent_model.stillOtherRelatedModel', $doublyNamedRelation[0]->getRelationName()); | ||
$this->assertEquals( | ||
'LEFT JOIN `belongs_to_related` AS `belongs_to_parent_model.stillOtherRelatedModel`' . | ||
' ON `belongs_to_parent_model`.`other_id` = `belongs_to_parent_model.stillOtherRelatedModel`.`other_id`', | ||
$doublyNamedRelation[0]->getStatement(), | ||
); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Tempest/Database/tests/Relations/Fixtures/BelongsToParentModel.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Database\Tests\Relations\Fixtures; | ||
|
||
use Tempest\Database\BelongsTo; | ||
use Tempest\Database\Builder\TableName; | ||
use Tempest\Database\DatabaseModel; | ||
use Tempest\Database\IsDatabaseModel; | ||
|
||
final class BelongsToParentModel implements DatabaseModel | ||
{ | ||
use IsDatabaseModel; | ||
|
||
public static function table(): TableName | ||
{ | ||
return new TableName('belongs_to_parent_model'); | ||
} | ||
|
||
public BelongsToRelatedModel $relatedModel; | ||
|
||
#[BelongsTo('other_id')] | ||
public BelongsToRelatedModel $otherRelatedModel; | ||
|
||
#[BelongsTo('other_id', 'other_id')] | ||
public BelongsToRelatedModel $stillOtherRelatedModel; | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Tempest/Database/tests/Relations/Fixtures/BelongsToRelatedModel.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Database\Tests\Relations\Fixtures; | ||
|
||
use Tempest\Database\Builder\TableName; | ||
use Tempest\Database\DatabaseModel; | ||
use Tempest\Database\HasMany; | ||
use Tempest\Database\IsDatabaseModel; | ||
|
||
final class BelongsToRelatedModel implements DatabaseModel | ||
{ | ||
use IsDatabaseModel; | ||
|
||
/** @var \Tempest\Database\Tests\Relations\Fixtures\BelongsToParentModel[] */ | ||
public array $inferred = []; | ||
|
||
#[HasMany('other_id')] | ||
/** @var \Tempest\Database\Tests\Relations\Fixtures\BelongsToParentModel[] */ | ||
public array $attribute = []; | ||
|
||
#[HasMany('other_id', BelongsToParentModel::class, 'other_id')] | ||
public array $full = []; | ||
|
||
/** @var \Tempest\Database\Tests\Relations\Fixtures\HasOneParentModel[] */ | ||
public array $invalid = []; | ||
|
||
public static function table(): TableName | ||
{ | ||
return new TableName('belongs_to_related'); | ||
} | ||
} |
Oops, something went wrong.