Belongs To (Many-to-One)

Belongs To (Many-to-One)

The BelongsTo relationship links one entity to another unique entity. For instance, if each post belongs to a user, this relationship can be annotated in the Post entity.

app/Entities/Post.php
<?php
 
declare(strict_types=1);
 
namespace App\Entities;
 
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Relation\BelongsTo;
 
#[Entity]
class Post
{
    #[Column(type: 'primary')]
    private $id;
 
    #[Column(type: 'string')]
    private $title;
 
    #[Column(type: 'text')]
    private $content;
 
    #[BelongsTo(target: 'App\Entities\User')]
    private $user;
}