-
-
Notifications
You must be signed in to change notification settings - Fork 14
Add RefersToMorphed relation attribute #123
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
19702ed
Add RefersToMorphed relation attribute
roxblnfk 6bc7d9f
Register RefersToMorphed attribute and bump cycle deps
roxblnfk 2d65668
Add RefersToMorphed to psalm baseline
roxblnfk b1172f6
Fix RefersToMorphed meta-storm argument indexes
roxblnfk a5aa920
Fix meta-storm argument indexes for all morphed relations
roxblnfk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,58 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Annotated\Annotation\Relation\Morphed; | ||
|
|
||
| use Cycle\Annotated\Annotation\Relation\Inverse; | ||
| use Cycle\Annotated\Annotation\Relation\Relation; | ||
| use Cycle\Annotated\Annotation\Relation\Traits\InverseTrait; | ||
| use Doctrine\Common\Annotations\Annotation\Target; | ||
| use JetBrains\PhpStorm\ExpectedValues; | ||
| use Spiral\Attributes\NamedArgumentConstructor; | ||
|
|
||
| /** | ||
| * Morphed variation of the refers-to relation. Like {@see BelongsToMorphed} it stores the outer key | ||
| * and the target role on the owner, but resolves the outer key in a deferred way, so it can be used | ||
| * for self-linked and cyclic morphed references. | ||
| * | ||
| * @Annotation | ||
| * @NamedArgumentConstructor | ||
| * @Target("PROPERTY") | ||
| */ | ||
| #[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor] | ||
| class RefersToMorphed extends Relation | ||
| { | ||
| use InverseTrait; | ||
|
|
||
| protected const TYPE = 'refersToMorphed'; | ||
|
|
||
| /** | ||
| * @param non-empty-string $target | ||
| * @param bool $cascade Automatically save related data with source entity. | ||
| * @param bool $nullable Defines if the relation can be nullable (child can have no parent). | ||
| * @param array|non-empty-string|null $innerKey Inner key in source entity. Defaults to `{relationName}_{outerKey}`. | ||
| * @param array|non-empty-string|null $outerKey Outer key in the related entity. Defaults to primary key. | ||
| * @param non-empty-string|null $morphKey Name of key to store related entity role. Defaults to `{relationName}_role`. | ||
| * @param int $morphKeyLength The length of morph key. | ||
| * @param bool $indexCreate Create an index on morphKey and innerKey. | ||
| * @param non-empty-string $load Relation load approach. | ||
| */ | ||
| public function __construct( | ||
| string $target, | ||
| protected bool $cascade = true, | ||
| protected bool $nullable = true, | ||
| protected array|string|null $innerKey = null, | ||
| protected array|string|null $outerKey = null, | ||
| protected ?string $morphKey = null, | ||
| protected int $morphKeyLength = 32, | ||
| protected bool $indexCreate = true, | ||
| #[ExpectedValues(values: ['lazy', 'eager'])] | ||
| string $load = 'lazy', | ||
| ?Inverse $inverse = null, | ||
| ) { | ||
| $this->inverse = $inverse; | ||
|
|
||
| parent::__construct($target, $load); | ||
| } | ||
| } |
This file contains hidden or 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 Cycle\Annotated\Tests\Fixtures\RefersToMorphed; | ||
|
|
||
| use Cycle\Annotated\Annotation\Column; | ||
| use Cycle\Annotated\Annotation\Entity; | ||
| use Cycle\Annotated\Annotation\Relation\Morphed\RefersToMorphed; | ||
|
|
||
| /** | ||
| * @Entity | ||
| */ | ||
| #[Entity] | ||
| class Comment | ||
| { | ||
| /** @Column(type="primary") */ | ||
| #[Column(type: 'primary')] | ||
| protected $id; | ||
|
|
||
| /** @Column(type="string") */ | ||
| #[Column(type: 'string')] | ||
| protected $message; | ||
|
|
||
| /** @RefersToMorphed(target="MorphedParentInterface") */ | ||
| #[RefersToMorphed(target: 'MorphedParentInterface')] | ||
| protected $parent; | ||
| } |
7 changes: 7 additions & 0 deletions
7
tests/Annotated/Fixtures/RefersToMorphed/MorphedParentInterface.php
This file contains hidden or 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,7 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Annotated\Tests\Fixtures\RefersToMorphed; | ||
|
|
||
| interface MorphedParentInterface {} |
This file contains hidden or 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,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Annotated\Tests\Fixtures\RefersToMorphed; | ||
|
|
||
| use Cycle\Annotated\Annotation\Column; | ||
| use Cycle\Annotated\Annotation\Entity; | ||
|
|
||
| /** | ||
| * @Entity | ||
| */ | ||
| #[Entity] | ||
| class Post implements MorphedParentInterface | ||
| { | ||
| /** @Column(type="primary") */ | ||
| #[Column(type: 'primary')] | ||
| protected $id; | ||
|
|
||
| /** @Column(type="string") */ | ||
| #[Column(type: 'string')] | ||
| protected $title; | ||
| } |
66 changes: 66 additions & 0 deletions
66
tests/Annotated/Functional/Driver/Common/Relation/Morphed/RefersToMorphedTestCase.php
This file contains hidden or 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,66 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Annotated\Tests\Functional\Driver\Common\Relation\Morphed; | ||
|
|
||
| use Cycle\Annotated\Entities; | ||
| use Cycle\Annotated\Locator\TokenizerEntityLocator; | ||
| use Cycle\Annotated\MergeColumns; | ||
| use Cycle\Annotated\MergeIndexes; | ||
| use Cycle\Annotated\Tests\Fixtures\RefersToMorphed\MorphedParentInterface; | ||
| use Cycle\Annotated\Tests\Functional\Driver\Common\BaseTestCase; | ||
| use Cycle\ORM\Relation; | ||
| use Cycle\ORM\SchemaInterface as Schema; | ||
| use Cycle\Schema\Compiler; | ||
| use Cycle\Schema\Generator\GenerateRelations; | ||
| use Cycle\Schema\Generator\GenerateTypecast; | ||
| use Cycle\Schema\Generator\RenderRelations; | ||
| use Cycle\Schema\Generator\RenderTables; | ||
| use Cycle\Schema\Generator\ResetTables; | ||
| use Cycle\Schema\Generator\SyncTables; | ||
| use Cycle\Schema\Registry; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Spiral\Attributes\ReaderInterface; | ||
| use Spiral\Tokenizer\Config\TokenizerConfig; | ||
| use Spiral\Tokenizer\Tokenizer; | ||
|
|
||
| abstract class RefersToMorphedTestCase extends BaseTestCase | ||
| { | ||
| #[DataProvider('allReadersProvider')] | ||
| public function testRelation(ReaderInterface $reader): void | ||
| { | ||
| $locator = (new Tokenizer(new TokenizerConfig([ | ||
| 'directories' => [__DIR__ . '/../../../../../Fixtures/RefersToMorphed'], | ||
| 'exclude' => [], | ||
| ])))->classLocator(); | ||
|
|
||
| $r = new Registry($this->dbal); | ||
|
|
||
| $schema = (new Compiler())->compile($r, [ | ||
| new Entities(new TokenizerEntityLocator($locator, $reader), $reader), | ||
| new ResetTables(), | ||
| new MergeColumns($reader), | ||
| new GenerateRelations(), | ||
| new RenderTables(), | ||
| new RenderRelations(), | ||
| new MergeIndexes($reader), | ||
| new SyncTables(), | ||
| new GenerateTypecast(), | ||
| ]); | ||
|
|
||
| $this->assertArrayHasKey('parent', $schema['comment'][Schema::RELATIONS]); | ||
| $this->assertSame( | ||
| Relation::REFERS_TO_MORPHED, | ||
| $schema['comment'][Schema::RELATIONS]['parent'][Relation::TYPE], | ||
| ); | ||
| $this->assertSame( | ||
| MorphedParentInterface::class, | ||
| $schema['comment'][Schema::RELATIONS]['parent'][Relation::TARGET], | ||
| ); | ||
|
|
||
| // Morphed refers-to stores the outer key and the target role on the source entity. | ||
| $this->assertContains('parent_id', $schema['comment'][Schema::COLUMNS]); | ||
| $this->assertContains('parent_role', $schema['comment'][Schema::COLUMNS]); | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
tests/Annotated/Functional/Driver/MySQL/Relation/Morphed/RefersToMorphedTest.php
This file contains hidden or 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,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Annotated\Tests\Functional\Driver\MySQL\Relation\Morphed; | ||
|
|
||
| // phpcs:ignore | ||
| use Cycle\Annotated\Tests\Functional\Driver\Common\Relation\Morphed\RefersToMorphedTestCase; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
|
|
||
| #[Group('driver')] | ||
| #[Group('driver-mysql')] | ||
| final class RefersToMorphedTest extends RefersToMorphedTestCase | ||
| { | ||
| public const DRIVER = 'mysql'; | ||
| } |
16 changes: 16 additions & 0 deletions
16
tests/Annotated/Functional/Driver/Postgres/Relation/Morphed/RefersToMorphedTest.php
This file contains hidden or 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,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Annotated\Tests\Functional\Driver\Postgres\Relation\Morphed; | ||
|
|
||
| // phpcs:ignore | ||
| use Cycle\Annotated\Tests\Functional\Driver\Common\Relation\Morphed\RefersToMorphedTestCase; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
|
|
||
| #[Group('driver')] | ||
| #[Group('driver-postgres')] | ||
| final class RefersToMorphedTest extends RefersToMorphedTestCase | ||
| { | ||
| public const DRIVER = 'postgres'; | ||
| } |
16 changes: 16 additions & 0 deletions
16
tests/Annotated/Functional/Driver/SQLServer/Relation/Morphed/RefersToMorphedTest.php
This file contains hidden or 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,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Annotated\Tests\Functional\Driver\SQLServer\Relation\Morphed; | ||
|
|
||
| // phpcs:ignore | ||
| use Cycle\Annotated\Tests\Functional\Driver\Common\Relation\Morphed\RefersToMorphedTestCase; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
|
|
||
| #[Group('driver')] | ||
| #[Group('driver-sqlserver')] | ||
| final class RefersToMorphedTest extends RefersToMorphedTestCase | ||
| { | ||
| public const DRIVER = 'sqlserver'; | ||
| } |
16 changes: 16 additions & 0 deletions
16
tests/Annotated/Functional/Driver/SQLite/Relation/Morphed/RefersToMorphedTest.php
This file contains hidden or 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,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Annotated\Tests\Functional\Driver\SQLite\Relation\Morphed; | ||
|
|
||
| // phpcs:ignore | ||
| use Cycle\Annotated\Tests\Functional\Driver\Common\Relation\Morphed\RefersToMorphedTestCase; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
|
|
||
| #[Group('driver')] | ||
| #[Group('driver-sqlite')] | ||
| final class RefersToMorphedTest extends RefersToMorphedTestCase | ||
| { | ||
| public const DRIVER = 'sqlite'; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.