Many-To-Many, Self-referencing SQL generating issue #5501

Closed
opened 2026-01-22 15:09:19 +01:00 by admin · 2 comments
Owner

Originally created by @evgenoid on GitHub (Apr 18, 2017).

I have described two fields at the BalanceStatement model:

    /**
     * @var Collection | BalanceStatement[]
     *
     * @ORM\ManyToMany(targetEntity="BalanceStatement", inversedBy="fundedBalanceStatements")
     * @ORM\JoinTable(name="balance_statement_fund",
     *      joinColumns={@ORM\JoinColumn(name="funded_by_balance_statement_id", onDelete="cascade")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="funded_balance_statement_id", onDelete="cascade")}
     * )
     */
    private $fundedByBalanceStatements;

    /**
     * @var Collection | BalanceStatement[]
     *
     * @ORM\ManyToMany(targetEntity="BalanceStatement", mappedBy="fundedByBalanceStatements")
     */
    private $fundedBalanceStatements;

And join it at query builder:

$qb->leftJoin('balanceStatement.fundedBalanceStatements', 'fundedBalanceStatements');

SQL generated as:

SELECT * FROM balance_statement b4_
LEFT JOIN balance_statement_fund b42_ ON b4_.id = b42_.funded_balance_statement_id 
LEFT JOIN balance_statement b21_ ON b21_.id = b42_.funded_by_balance_statement_id 

but expected SQL should be:

SELECT * FROM balance_statement b4_
LEFT JOIN balance_statement_fund b42_ ON b4_.id = b42_.funded_by_balance_statement_id 
LEFT JOIN balance_statement b21_ ON b21_.id = b42_.funded_balance_statement_id 
Originally created by @evgenoid on GitHub (Apr 18, 2017). I have described two fields at the BalanceStatement model: ```php /** * @var Collection | BalanceStatement[] * * @ORM\ManyToMany(targetEntity="BalanceStatement", inversedBy="fundedBalanceStatements") * @ORM\JoinTable(name="balance_statement_fund", * joinColumns={@ORM\JoinColumn(name="funded_by_balance_statement_id", onDelete="cascade")}, * inverseJoinColumns={@ORM\JoinColumn(name="funded_balance_statement_id", onDelete="cascade")} * ) */ private $fundedByBalanceStatements; /** * @var Collection | BalanceStatement[] * * @ORM\ManyToMany(targetEntity="BalanceStatement", mappedBy="fundedByBalanceStatements") */ private $fundedBalanceStatements; ``` And join it at query builder: ```php $qb->leftJoin('balanceStatement.fundedBalanceStatements', 'fundedBalanceStatements'); ``` SQL generated as: ```SQL SELECT * FROM balance_statement b4_ LEFT JOIN balance_statement_fund b42_ ON b4_.id = b42_.funded_balance_statement_id LEFT JOIN balance_statement b21_ ON b21_.id = b42_.funded_by_balance_statement_id ``` but expected SQL should be: ```SQL SELECT * FROM balance_statement b4_ LEFT JOIN balance_statement_fund b42_ ON b4_.id = b42_.funded_by_balance_statement_id LEFT JOIN balance_statement b21_ ON b21_.id = b42_.funded_balance_statement_id ```
admin closed this issue 2026-01-22 15:09:19 +01:00
Author
Owner

@szymach commented on GitHub (Apr 18, 2017):

If I am looking correctly, you did not provide the second join call in the query builder?

@szymach commented on GitHub (Apr 18, 2017): If I am looking correctly, you did not provide the second join call in the query builder?
Author
Owner

@beberlei commented on GitHub (Dec 8, 2020):

Closed due to lack of details.

@beberlei commented on GitHub (Dec 8, 2020): Closed due to lack of details.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5501