'JoinColumns/Composite keys with php attributes

How do you declare joinColumns/composite keys with PHP attributes. Haven't been able to find the right way and it is not documented (https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/reference/attributes-reference.html)

Entities

Comment.php

#[ORM\Entity(repositoryClass: CommentRepository::class)]
class Comment
{
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\ManyToOne(targetEntity: PullRequest::class, inversedBy: 'comments')]
    #[ORM\JoinColumn(name: 'pull_request_id', referencedColumnName: 'id')]
    #[ORM\JoinColumn(name: 'repo_id', referencedColumnName: 'repo_id')]
    private $pullRequest;
}

PullRequest.php

#[ORM\Entity(repositoryClass: PullRequestRepository::class)]
class PullRequest
{
    #[ORM\Id]
    #[ORM\Column(type: 'integer', unique: false)]
    private $id;

    #[ORM\Id]
    #[ORM\ManyToOne(targetEntity: Repo::class, inversedBy: 'pullRequests')]
    #[ORM\JoinColumn(nullable: false)]
    private $repo;

    #[ORM\OneToMany(mappedBy: 'pullRequest', targetEntity: Comment::class, orphanRemoval: true)]
    private $comments;

}


Solution 1:[1]

I ran into the same issue today and managed to find a workaround. It indeed seems like JoinColumns is not available as a PHP8 attribute, at least not at Doctrine ORM 2.11 nor the upcoming 2.12 / 3.0.

However, you can work around this by moving the join columns definitions into an AssociationsOverride attribute at the class level as follows:

#[ORM\Entity(repositoryClass: CommentRepository::class)]
#[ORM\AssociationOverrides([
    new ORM\AssociationOverride(
        name: 'pullRequest',
        joinColumns: [
            new ORM\JoinColumn(name: 'pull_request_id', referencedColumnName: 'id'),
            new ORM\JoinColumn(name: 'repo_id', referencedColumnName: 'repo_id')
        ]
    )
])]
class Comment
{
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\ManyToOne(targetEntity: PullRequest::class, inversedBy: 'comments')]
    private $pullRequest;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marc Puts