Project

General

Profile

Actions

Bug #105615

closed

Multiple conditions for QueryBuilder leftJoin not possible (again)

Added by Bastian Stargazer 4 days ago. Updated 2 days ago.

Status:
Closed
Priority:
Should have
Assignee:
-
Category:
Database API (Doctrine DBAL)
Target version:
Start date:
2024-11-17
Due date:
% Done:

0%

Estimated time:
TYPO3 Version:
11
PHP Version:
8.1
Tags:
Complexity:
easy
Is Regression:
Sprint Focus:

Description

In our current system (v11.5.41) it seems again not possible, to add multiple conditions in joins (see old and solved issue https://forge.typo3.org/issues/87411).

The reason is, that the condition parameter is again typed as string:

public function leftJoin(string $fromAlias, string $join, string $alias, ?string $condition = null): QueryBuilder

Probably in later versions like current v12 and v13 it is the same? How should we deal with this?

Actions #1

Updated by Garvin Hicking 4 days ago

  • Status changed from New to Needs Feedback

Are you talking about the Extbase or core Querybuilder? What is your exact code that you use?

The querybuilder has __toString() casting that can convert things, so this shouldn't happen when joins are properly built.

Actions #2

Updated by Bastian Stargazer 3 days ago

Thanks for your fast response. We're using the core QueryBuilder, created by:

$qb = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tablename');

Indeed it works if we manually add a string-cast in front of the query-builder object.
Is this meant to be like this, or should toString() called automatically?

->from('fe_users', 'u')
->leftJoin('u', 'example', 'e', (string)$qb->expr()->andX(
    $qb->expr()->eq('u.uid', 'e.fe_users_uid'),
    $qb->expr()->eq('e.field', 1),
))

Actions #3

Updated by Garvin Hicking 3 days ago

You're welcome :)

In fact, yes - the T3v11 core did this using (string) too:

https://github.com/TYPO3/typo3/blob/054dda798e88a28f97df6b3b84ea2cc0fb425c7a/typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbQueryParser.php#L1015

(Please note in TYPO3v12+, the andX() methods are replaced with simple and() methods, but the string cast remains).

If that's ok with you, I would close the ticket?

Actions #4

Updated by Bastian Stargazer 2 days ago

All right, if the core use the string-cast too, we'll consider it as best-practice ;-)

I was just wondering, because everything becomes more and more type-save. So inside the join() method it would be easy to check if the given parameter is an object of type CompositeExpression (or ExpressionBuilder?!) and just call toString() in this case.

Anyway, casting is fine, just have to remember this case. Maybe a hint or example in the docs make sense. I mean, the current example (https://docs.typo3.org/permalink/t3coreapi:ApiOverview/Database/QueryBuilder/Index#join-innerjoin-rightjoin-and-leftjoin) could be easily extended by adding an and() condition as 4th parameter in this join(). But its extra-sugar.

(Please note in TYPO3v12+, the andX() methods are replaced with simple and() methods, but the string cast remains).
If that's ok with you, I would close the ticket?

Highly appreciate your feedback! 🙏🏼 And yes, you can close it.

Actions #5

Updated by Garvin Hicking 2 days ago

  • Status changed from Needs Feedback to Closed

I was just wondering, because everything becomes more and more type-save. So inside the join() method it would be easy to check if the given parameter is an object of type CompositeExpression (or ExpressionBuilder?!) and just call toString() in this case.

This is a good feeling. However for SQL, this is all string-based, so once a query gets built, this is the target format and it's "ok" to resolve the specific objects into strings. You could even pass "AND/OR" join conditions strings yourself, but due to escaping/quoting that isn't a good idea. Thus, using the (string) casting is really your best bet.

Anyway, casting is fine, just have to remember this case. Maybe a hint or example in the docs make sense. I mean, the current example (https://docs.typo3.org/permalink/t3coreapi:ApiOverview/Database/QueryBuilder/Index#join-innerjoin-rightjoin-and-leftjoin) could be easily extended by adding an and() condition as 4th parameter in this join(). But its extra-sugar.

Yes, you are completely right! I've just added a PR to address this in the docs: https://github.com/TYPO3-Documentation/TYPO3CMS-Reference-CoreApi/pull/5062

Thanks!

Actions

Also available in: Atom PDF