DDC-1239: Using a repository with composite keys generates invalid SQL #1562

Closed
opened 2026-01-22 13:18:11 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 30, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user spiffyjr:

Invalid SQL is generated when using composite keys along with Repository methods (fineOneBy, etc). Everything works properly when using createQuery() to generate DQL manually.

Working:
$q = $this->_em
->createQuery(
"SELECT c,c2,g FROM Entity\OAuth\Consumer c LEFT JOIN c.group g LEFT JOIN c.clinic c2 WHERE c.key = '{$consumer_key}'");
$this->_consumer = $q->getSingleResult();

Invalid:
$this->_consumer = $this->_em->getRepository('Entity\OAuth\Consumer')
->findOneBy(array('key' => $consumer_key));

Generated SQL for "invalid" method:
SELECT ... FROM oauth_consumer t0 LEFT JOIN groop t10 ON t0.groupId = t10.id LEFT JOIN company t17 ON t0.companyId = t17.id LEFT JOIN clinic t28 ON t0.dsid = t28.dsid t0.rid = t28.rid LEFT JOIN user t50 ON t0.userId = t50.id WHERE t0.key = ?

Notice the missing AND in the query above. I've attached the entities in question.

Originally created by @doctrinebot on GitHub (Jun 30, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user spiffyjr: Invalid SQL is generated when using composite keys along with Repository methods (fineOneBy, etc). Everything works properly when using createQuery() to generate DQL manually. Working: $q = $this->_em ->createQuery( "SELECT c,c2,g FROM Entity\OAuth\Consumer c LEFT JOIN c.group g LEFT JOIN c.clinic c2 WHERE c.key = '{$consumer_key}'"); $this->_consumer = $q->getSingleResult(); Invalid: $this->_consumer = $this->_em->getRepository('Entity\OAuth\Consumer') ->findOneBy(array('key' => $consumer_key)); Generated SQL for "invalid" method: SELECT ... FROM oauth_consumer t0 LEFT JOIN groop t10 ON t0.groupId = t10.id LEFT JOIN company t17 ON t0.companyId = t17.id LEFT JOIN clinic t28 **ON t0.dsid = t28.dsid t0.rid = t28.rid** LEFT JOIN user t50 ON t0.userId = t50.id WHERE t0.`key` = ? Notice the missing **AND** in the query above. I've attached the entities in question.
admin added the Bug label 2026-01-22 13:18:11 +01:00
admin closed this issue 2026-01-22 13:18:13 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jun 30, 2011):

Comment created by @beberlei:

Fixed

@doctrinebot commented on GitHub (Jun 30, 2011): Comment created by @beberlei: Fixed
Author
Owner

@doctrinebot commented on GitHub (Jun 30, 2011):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Jun 30, 2011): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Dec 13, 2015):

Imported 2 attachments from Jira into https://gist.github.com/fe2f01013861371cfb9f

@doctrinebot commented on GitHub (Dec 13, 2015): Imported 2 attachments from Jira into https://gist.github.com/fe2f01013861371cfb9f - [11021_Clinic.php](https://gist.github.com/fe2f01013861371cfb9f#file-11021_Clinic-php) - [11022_Consumer.php](https://gist.github.com/fe2f01013861371cfb9f#file-11022_Consumer-php)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1562