Answer the question
In order to leave comments, you need to log in
How to create an entity linked in a custom table?
Suggest a solution.
If two entities A and B are interconnected (many-to-many), three tables A, B and A_B are obtained.
I need to display all A, while counting how many B are connected to them.
This is not difficult to do, since there are properties inversedBy and mappedBy, but the problem of the doctrine is that it makes an extra join.
More or less like this:
SELECT A.*, count(DISTINCT Б.ID) FROM A
LEFT JOIN А_Б ON А_Б.A_ID = A.ID
LEFT JOIN Б ON Б.ID = А_Б.Б_ID
SELECT A.*, count(DISTINCT А_Б.Б_ID) FROM A
LEFT JOIN А_Б ON А_Б.A_ID = A.ID
Many hours of magic
@ORM\MappedSuperclass, Entity(readOnly=true), @ORM\JoinColumn, @ORM\Column, mappedBy, inversedBy
/**
* @var integer
* @ORM\JoinColumn(name="b_id")
*/
protected $bId;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question