-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91: expose a Foreign Key definition via attribute
- Loading branch information
Showing
24 changed files
with
755 additions
and
15 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
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Annotated\Annotation; | ||
|
||
use Doctrine\Common\Annotations\Annotation\Target; | ||
use JetBrains\PhpStorm\ExpectedValues; | ||
use Spiral\Attributes\NamedArgumentConstructor; | ||
|
||
/** | ||
* @Annotation | ||
* | ||
* @NamedArgumentConstructor | ||
* | ||
* @Target({"PROPERTY", "ANNOTATION", "CLASS"}) | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)] | ||
#[NamedArgumentConstructor] | ||
class ForeignKey | ||
{ | ||
/** | ||
* @param non-empty-string $target Role or class name of the target entity. | ||
* @param list<non-empty-string>|non-empty-string|null $innerKey You don't need to specify this if the attribute | ||
* is used on a property. | ||
* @param list<non-empty-string>|non-empty-string|null $outerKey Outer key in the target entity. | ||
* Defaults to the primary key. | ||
* @param 'CASCADE'|'NO ACTION'|'SET null' $action | ||
* @param bool $indexCreate Note: MySQL and MSSQL might create an index for the foreign key automatically. | ||
*/ | ||
public function __construct( | ||
public string $target, | ||
public array|string|null $innerKey = null, | ||
public array|string|null $outerKey = null, | ||
/** | ||
* @Enum({"NO ACTION", "CASCADE", "SET NULL"}) | ||
*/ | ||
#[ExpectedValues(values: ['NO ACTION', 'CASCADE', 'SET NULL'])] | ||
public string $action = 'CASCADE', | ||
public bool $indexCreate = true, | ||
) { | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
tests/Annotated/Fixtures/Fixtures24/Class/DatabaseField/DatabaseField.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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Annotated\Tests\Fixtures\Fixtures24\Class\DatabaseField; | ||
|
||
use Cycle\Annotated\Annotation\Column; | ||
use Cycle\Annotated\Annotation\Entity; | ||
use Cycle\Annotated\Annotation\ForeignKey; | ||
|
||
/** | ||
* @Entity(role="from", table="from") | ||
* @ForeignKey(target="target", outerKey="outer_key", innerKey="inner_key") | ||
*/ | ||
#[ForeignKey(target: Target::class, innerKey: 'inner_key', outerKey: 'outer_key')] | ||
#[Entity(role: 'from', table: 'from')] | ||
class DatabaseField | ||
{ | ||
/** | ||
* @Column(type="primary") | ||
*/ | ||
#[Column(type: 'primary')] | ||
public int $id; | ||
|
||
/** | ||
* @Column(type="integer", name="inner_key") | ||
*/ | ||
#[Column(type: 'integer', name: 'inner_key')] | ||
public int $innerKey; | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/Annotated/Fixtures/Fixtures24/Class/DatabaseField/Target.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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Annotated\Tests\Fixtures\Fixtures24\Class\DatabaseField; | ||
|
||
use Cycle\Annotated\Annotation\Column; | ||
use Cycle\Annotated\Annotation\Entity; | ||
|
||
/** | ||
* @Entity(table="to") | ||
*/ | ||
#[Entity(table: 'to')] | ||
class Target | ||
{ | ||
/** | ||
* @Column(type="primary") | ||
*/ | ||
#[Column(type: 'primary')] | ||
public int $id; | ||
|
||
/** | ||
* @Column(type="integer", name="outer_key") | ||
*/ | ||
#[Column(type: 'integer', name: 'outer_key')] | ||
public int $outerKey; | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/Annotated/Fixtures/Fixtures24/Class/PrimaryKey/PrimaryKey.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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Annotated\Tests\Fixtures\Fixtures24\Class\PrimaryKey; | ||
|
||
use Cycle\Annotated\Annotation\Column; | ||
use Cycle\Annotated\Annotation\Entity; | ||
use Cycle\Annotated\Annotation\ForeignKey; | ||
|
||
/** | ||
* @Entity(role="from", table="from") | ||
* @ForeignKey(target="target", outerKey="id", innerKey="inner_key") | ||
*/ | ||
#[ForeignKey(target: Target::class, outerKey: 'id', innerKey: 'inner_key')] | ||
#[Entity(role: 'from', table: 'from')] | ||
class PrimaryKey | ||
{ | ||
/** | ||
* @Column(type="primary") | ||
*/ | ||
#[Column(type: 'primary')] | ||
public int $id; | ||
|
||
/** | ||
* @Column(type="integer", name="inner_key") | ||
*/ | ||
#[Column(type: 'integer', name: 'inner_key')] | ||
public int $innerKey; | ||
} |
Oops, something went wrong.