v3.4.0
What's Changed
- Removing unused import by @msmakouz in #90
- Expose a Foreign Key definition via the new
ForeignKey
attribute by @msmakouz in #91
Foreign Key declaration via attributes
Now, you can easily define foreign keys in your entity classes using attributes, without needing any relation definitions.
How It Works:
-
Define Entities using the
#[Entity]
attribute. For example, aUser
entity can be created with an ID as the primary key.use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\Column; #[Entity] class User { #[Column(type: 'primary')] public int $id; }
-
Set up Foreign Keys in two ways:
-
Directly on Properties: You can annotate a property in your class to indicate it's a foreign key related to another entity.
use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\ForeignKey; #[Entity] class Post { // ... #[Column(type: 'integer')] #[ForeignKey(target: User::class, action: 'CASCADE')] private int $userId; }
-
Or, on the Class Itself: Alternatively, you can place the foreign key annotation at the class level, specifying details like which property in your class is the foreign key (
innerKey
), and which property in the related class it corresponds to (outerKey
).use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\ForeignKey; #[Entity] #[ForeignKey(target: User::class, innerKey: 'userId', outerKey: 'id', action: 'CASCADE')] class Post { // ... #[Column(type: 'integer')] private int $userId; }
Full Changelog: v3.3.1...v3.4.0