Bug #62151
closedaddUnionStatement does not handle multiple fields with same related table
0%
Description
When a domain model has more than 1 field which are related to the same table you cannot create a query checking both fields.
Example:
Domain\Model\ContentDraft (content_draft)
- creator: Domain\Model\FrontendUser (fe_users)
- editor: Domain\Model\FrontendUser (fe_users)
To search for drafts for a user name, a query may be created like this in Domain\Repository\ContentDraft:
$query = $this->createQuery() ; $constraints = array() ; $constraints[] = $query->like('creator.name', 'name1') ; $constraints[] = $query->like('editor.name', 'name2') ; $query->matching( $query->logicalOr($constraints) ) ;
When the query is build in Typo3DbBackend there's only one JOIN clause for fe_users and two WHERE conditions which target the same (joined) table. Simplified query:
SELECT * FROM content_draft.* LEFT JOIN fe_users ON content_draft.editor=fe_users.uid WHERE fe_users.name LIKE '%name1%' OR fe_users.name LIKE '%name2%'
Though it should be something like this:
SELECT * FROM content_draft.* LEFT JOIN fe_users AS fe_users1 ON content_draft.creator=fe_users.uid LEFT JOIN fe_users AS fe_users2 ON content_draft.editor=fe_users.uid WHERE fe_users1.name LIKE '%name1%' OR fe_users2.name LIKE '%name2%'
In Typo3DbBackend::addUnionStatement() the join clauses are added to $sql['unions'] but every time $childTableName is used as key, so only the join clause for the last constraint is saved in the array and eventually added to the query. Also the where conditions do all use fe_users and do not differntiate for the multiple constraints.