[PR #6812] Fix syntax error when join unrelated entity with discriminator entity #10153

Closed
opened 2026-01-22 16:06:36 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/6812

State: closed
Merged: Yes


When joining a unrelated entity with a discriminator the join contains the table name and alias in brackets which is a syntax error (at least in SQL Server).

Exmaple:

SELECT
  c
FROM
  Doctrine\Tests\Models\Company\CompanyContract c
JOIN
  c.salesPerson s
LEFT JOIN
  Doctrine\Tests\Models\Company\CompanyEvent e WITH s.id = e.id

Results in:

SELECT
  c0_.id AS id_0,
  c0_.completed AS completed_1,
  c0_.fixPrice AS fixPrice_2,
  c0_.hoursWorked AS hoursWorked_3,
  c0_.pricePerHour AS pricePerHour_4,
  c0_.maxPrice AS maxPrice_5,
  c0_.discr AS discr_6
FROM
  company_contracts c0_
INNER JOIN
  company_employees c1_ ON c0_.salesPerson_id = c1_.id
LEFT JOIN
  company_persons c2_ ON c1_.id = c2_.id
LEFT JOIN
  (company_events c3_) ON (c2_.id = c3_.id)
WHERE
  c0_.discr IN ('fix', 'flexible', 'flexultra')

After the fix it results in:

SELECT
  c0_.id AS id_0,
  c0_.completed AS completed_1,
  c0_.fixPrice AS fixPrice_2,
  c0_.hoursWorked AS hoursWorked_3,
  c0_.pricePerHour AS pricePerHour_4,
  c0_.maxPrice AS maxPrice_5,
  c0_.discr AS discr_6
FROM
  company_contracts c0_
INNER JOIN
  company_employees c1_ ON c0_.salesPerson_id = c1_.id
LEFT JOIN
  company_persons c2_ ON c1_.id = c2_.id
LEFT JOIN
  company_events c3_ ON (c2_.id = c3_.id)
WHERE
  c0_.discr IN ('fix', 'flexible', 'flexultra')

I have created #6811 PR for backporting this to the 2.5 branch as well.

**Original Pull Request:** https://github.com/doctrine/orm/pull/6812 **State:** closed **Merged:** Yes --- When joining a unrelated entity with a discriminator the join contains the table name and alias in brackets which is a syntax error (at least in SQL Server). Exmaple: ```sql SELECT c FROM Doctrine\Tests\Models\Company\CompanyContract c JOIN c.salesPerson s LEFT JOIN Doctrine\Tests\Models\Company\CompanyEvent e WITH s.id = e.id ``` Results in: ```sql SELECT c0_.id AS id_0, c0_.completed AS completed_1, c0_.fixPrice AS fixPrice_2, c0_.hoursWorked AS hoursWorked_3, c0_.pricePerHour AS pricePerHour_4, c0_.maxPrice AS maxPrice_5, c0_.discr AS discr_6 FROM company_contracts c0_ INNER JOIN company_employees c1_ ON c0_.salesPerson_id = c1_.id LEFT JOIN company_persons c2_ ON c1_.id = c2_.id LEFT JOIN (company_events c3_) ON (c2_.id = c3_.id) WHERE c0_.discr IN ('fix', 'flexible', 'flexultra') ``` After the fix it results in: ```sql SELECT c0_.id AS id_0, c0_.completed AS completed_1, c0_.fixPrice AS fixPrice_2, c0_.hoursWorked AS hoursWorked_3, c0_.pricePerHour AS pricePerHour_4, c0_.maxPrice AS maxPrice_5, c0_.discr AS discr_6 FROM company_contracts c0_ INNER JOIN company_employees c1_ ON c0_.salesPerson_id = c1_.id LEFT JOIN company_persons c2_ ON c1_.id = c2_.id LEFT JOIN company_events c3_ ON (c2_.id = c3_.id) WHERE c0_.discr IN ('fix', 'flexible', 'flexultra') ``` I have created #6811 PR for backporting this to the 2.5 branch as well.
admin added the pull-request label 2026-01-22 16:06:36 +01:00
admin closed this issue 2026-01-22 16:06:37 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#10153