Skip to content

v3.4.0

Compare
Choose a tag to compare
@roxblnfk roxblnfk released this 23 Nov 14:51
· 1 commit to 3.x since this release
bb376d2

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:

  1. Define Entities using the #[Entity] attribute. For example, a User 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;
    }
  2. 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