Doctrine many-to-many relationship with value in the middle

Viewed 31

I have a many-to-many relationship like this:

enter image description here

Everything is pretty classic with the only difference that classified_attribute table has a value column, idea behind it is pretty straightforward - to have a flexible list where i can map any attribute to any ad and set a value to it, and using SQL i am getting results i want:

SELECT
    `classified`.`name`,
    `attribute`.`name`,
    `classified_attribute`.`value`
FROM `classified`
LEFT JOIN classified_attribute
ON classified.id = classified_attribute.classified_id
LEFT JOIN attribute
    ON classified_attribute.attribute_id = attribute.id

Results:

enter image description here

But now i don't understand how to reproduce these results using Doctrine

#[ORM\Entity(repositoryClass: ClassifiedRepository::class)]
#[ORM\Table(name: 'classified')]
class Classified
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private int $id;

    #[ORM\Column(length: 100)]
    private string $name;


    #[ORM\ManyToMany(targetEntity: Attribute::class)]
    #[ORM\JoinTable(name: 'classified_attribute')]
    #[ORM\JoinColumn(name: 'attribute_id', referencedColumnName: 'id')]
    #[ORM\InverseJoinColumn(name: 'classified_id', referencedColumnName: 'id')]
    private Collection $attributes;

    # getters setters
}

I do get a list of attributes, but of course it's without value column, since attribute entity is mapped to attribute table which doesn't have it.

I've found a solution for Java: https://www.baeldung.com/jpa-many-to-many#many-to-many-using-a-composite-key, but is sit possible in Doctrine?

0 Answers
Related